PluginProbe
Canonical Link / trunk
Canonical Link vtrunk
canonical-link / canonical-link.php

canonical-link.php in Canonical Link trunk, at canonical-link.php

62 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Canonical Link
4 Plugin URI: https://webguy.io/blog/super-simple-dynamic-canonical-link-code/
5 Description: Adds the canonical link to your site (https://wikipedia.org/wiki/Canonical_link_element). Activate and then set your permalinks to "Post name" under Settings > Permalinks. That's it. Verify that it's working correctly with the Firefox add-on: https://addons.mozilla.org/addon/canonical-link/.
6 Version: 1.7
7 Requires at least: 5.0
8 Author: Web Guy
9 Author URI: https://webguy.io/
10 License: CC0
11 License URI: https://creativecommons.org/public-domain/cc0/
12 Text Domain: canonical-link
13 */
14
15 if ( !defined( 'ABSPATH' ) ) {
16 http_response_code( 404 );
17 die();
18 }
19
20 if ( is_admin() ) {
21 add_action( 'add_meta_boxes', 'canonicalink_meta_box_add' );
22 function canonicalink_meta_box_add() {
23 $post_types = get_post_types( array( 'public' => true ) );
24 add_meta_box( 'canonicalink_post', 'Canonical Link', 'canonicalink_post_meta_box', $post_types, 'normal', 'high' );
25 }
26 function canonicalink_post_meta_box( $post ) {
27 $values = get_post_custom( $post->ID );
28 $canonicalink_custom_url = '';
29 if ( isset( $values['canonicalink_custom'] ) ) {
30 $canonicalink_custom_url = esc_url( $values['canonicalink_custom'][0] );
31 }
32 wp_nonce_field( 'canonicalink_meta_box_nonce', 'meta_box_nonce' );
33 ?>
34 <p><input type="text" name="canonicalink_custom" class="large-text" placeholder="https://example.com/" value="<?php if ( $canonicalink_custom = get_post_meta( $post->ID, 'canonicalink_custom', true ) ) { echo esc_url( $canonicalink_custom_url ); } ?>"></p>
35 <?php
36 }
37 add_action( 'save_post', 'canonicalink_meta_box_save' );
38 function canonicalink_meta_box_save( $post_id ) {
39 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
40 if ( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['meta_box_nonce'] ) ), 'canonicalink_meta_box_nonce' ) ) return;
41 if ( !current_user_can( 'edit_post', $post_id ) ) return;
42 if ( isset( $_POST['canonicalink_custom'] ) ) {
43 update_post_meta( $post_id, 'canonicalink_custom', esc_url_raw( wp_unslash( $_POST['canonicalink_custom'] ) ) );
44 }
45 }
46 }
47
48 add_action( 'wp_head', 'canonicalink', 1 );
49 function canonicalink() {
50 global $post;
51 if ( !$post ) return;
52 $canonicalink_custom_url = '';
53 $values = get_post_custom( $post->ID );
54 if ( isset( $values['canonicalink_custom'] ) ) {
55 $canonicalink_custom_url = esc_url( $values['canonicalink_custom'][0] );
56 }
57 if ( ( is_singular() ) && ( $canonicalink_custom = get_post_meta( $post->ID, 'canonicalink_custom', true ) ) ) {
58 echo '<link rel="canonical" href="' . esc_url( $canonicalink_custom_url ) . '">';
59 } else {
60 echo '<link rel="canonical" href="' . esc_url( ( isset( $_SERVER['HTTPS'] ) ? 'https' : 'http' ) . '://' . ( isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : '' ) . wp_parse_url( isset( $_SERVER['REQUEST_URI'] ) ? sanitize_url( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '', PHP_URL_PATH ) ) . '">';
61 }
62 }