最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp head - Custom plugin - $post_id in wp_head

matteradmin10PV0评论

(english is not my language so I'm sorry if I don't explain really good)

My custom plugin get an URL from custom metabox and add it into the header.php My problem is that I can't get $post_id in my wp_head function to insert the URL in the header (all works fine before this).

Add my code from my custom plugin:

add_action( 'add_meta_boxes', 'extra_metabox' );

function extra_metabox () {

add_meta_box( 'extra-metabox', 'Extra Meta Box', 'extra_create_mb', array('page','post'), 'normal', 'core' );
}

function extra_create_mb( $post ){

wp_nonce_field( 'wpe_extra_nonce_save', 'wpe_extra_nonce' );

$meta_alternate = get_post_meta( $post->ID, 'extra_meta_alternate', true );

$html=
    '<table width="100%">
        <tr>
            <td style="width: 20%" valign="top"><label for="extra_meta_alternate">'.__('Alternate', 'extra_custom_alternate_redirect').'</label></td>
            <td>
                <input type="url" id="extra_meta_alternate" name="extra_meta_alternate" pattern="https://.*" value="' . esc_attr( $meta_alternate ) . '" style="width: 100%;">
            </td>
        </tr>
    </table>';


function extra_save_mb( $post_id ) {

$nonce_name   = isset( $_POST['wpe_extra_nonce'] ) ? $_POST['wpe_extra_nonce'] : '';
$nonce_action = 'wpe_extra_nonce_save';

if ( ! isset( $nonce_name ) ) {
    return;
}

if ( ! wp_verify_nonce( $nonce_name, $nonce_action ) ) {
    return;
}

if ( wp_is_post_revision( $post_id ) ) {
    return;
}

update_post_meta( $post_id, 'extra_meta_alternate', sanitize_text_field($_POST['extra_meta_alternate'] ) );

 }
add_action( 'save_post', 'extra_save_mb' );


function extra_head(){
//global $post;
$meta_alternate = get_post_meta( $post->ID, 'extra_meta_alternate', true );
//$meta_alternate = get_post_meta( $post_id, 'extra_meta_alternate', true );
//$meta_alternate = get_post_meta( get_the_ID(), 'extra_meta_alternate', true );
if ( is_page() || is_single() ){

        echo '<link rel="alternate" media="only screen and (max-width:400px)" href="'.$meta_alternate.'" />';   
        }   
}
add_action ('wp_head', 'extra_head');

But in <head></head> only print:

<link rel="alternate" media="only screen and (max-width: 400px)" href="">

Also I try to add global $post inside extra_head() or get_the_ID()but didn't work.

I'm relatively new with this, so thanks for the help.

(english is not my language so I'm sorry if I don't explain really good)

My custom plugin get an URL from custom metabox and add it into the header.php My problem is that I can't get $post_id in my wp_head function to insert the URL in the header (all works fine before this).

Add my code from my custom plugin:

add_action( 'add_meta_boxes', 'extra_metabox' );

function extra_metabox () {

add_meta_box( 'extra-metabox', 'Extra Meta Box', 'extra_create_mb', array('page','post'), 'normal', 'core' );
}

function extra_create_mb( $post ){

wp_nonce_field( 'wpe_extra_nonce_save', 'wpe_extra_nonce' );

$meta_alternate = get_post_meta( $post->ID, 'extra_meta_alternate', true );

$html=
    '<table width="100%">
        <tr>
            <td style="width: 20%" valign="top"><label for="extra_meta_alternate">'.__('Alternate', 'extra_custom_alternate_redirect').'</label></td>
            <td>
                <input type="url" id="extra_meta_alternate" name="extra_meta_alternate" pattern="https://.*" value="' . esc_attr( $meta_alternate ) . '" style="width: 100%;">
            </td>
        </tr>
    </table>';


function extra_save_mb( $post_id ) {

$nonce_name   = isset( $_POST['wpe_extra_nonce'] ) ? $_POST['wpe_extra_nonce'] : '';
$nonce_action = 'wpe_extra_nonce_save';

if ( ! isset( $nonce_name ) ) {
    return;
}

if ( ! wp_verify_nonce( $nonce_name, $nonce_action ) ) {
    return;
}

if ( wp_is_post_revision( $post_id ) ) {
    return;
}

update_post_meta( $post_id, 'extra_meta_alternate', sanitize_text_field($_POST['extra_meta_alternate'] ) );

 }
add_action( 'save_post', 'extra_save_mb' );


function extra_head(){
//global $post;
$meta_alternate = get_post_meta( $post->ID, 'extra_meta_alternate', true );
//$meta_alternate = get_post_meta( $post_id, 'extra_meta_alternate', true );
//$meta_alternate = get_post_meta( get_the_ID(), 'extra_meta_alternate', true );
if ( is_page() || is_single() ){

        echo '<link rel="alternate" media="only screen and (max-width:400px)" href="'.$meta_alternate.'" />';   
        }   
}
add_action ('wp_head', 'extra_head');

But in <head></head> only print:

<link rel="alternate" media="only screen and (max-width: 400px)" href="">

Also I try to add global $post inside extra_head() or get_the_ID()but didn't work.

I'm relatively new with this, so thanks for the help.

Share Improve this question asked Oct 17, 2018 at 12:51 ExeExe 1135 bronze badges 1
  • Please check this its related to your question stackoverflow/questions/22351038/… – Pratik Patel Commented Oct 17, 2018 at 12:59
Add a comment  | 

1 Answer 1

Reset to default 3

Use get_queried_object_id() to get the post ID of the current page/post when viewing a single page or post, regardless of whether or not you're in the loop, or what the global $post variable happens to be at that moment:

if ( is_page() || is_single() ) {
    $post_id        = get_queried_object_id();
    $meta_alternate = get_post_meta( $post_id, 'extra_meta_alternate', true );

    echo '<link rel="alternate" media="only screen and (max-width:400px)" href="' . $meta_alternate. '" />';   
}

Note that I moved the get_queried_object_id() and get_post_meta() to inside the if statement. This is because if you're not on a page or post, the queried object ID could be the ID of a category or tag. This makes sure you're only getting the ID if you're on a post or page.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far