psudo
psudo

Reputation: 1558

Place custom field data in header section

I'm a newbie to the Wordpress Customization and learning plugin development from a YouTube videos.

Below is the code snippet from my custom plugin that adds the two custom fields meta_title and meta_description in the example create/ edit form.

function ideapro_custom_posttype() {
    register_post_type( 'Example', 
        array(
           'labels'=>array(
              'name' =>__('Examples'),
              'singular_name' => __('Example'),
              'add_new' => __('Add New Example'),
              'add_new_item' => __('Add New Example'),
              'edit_item' => __('Edit Example'),
              'search_items' => __('Search Examples')
           ),
           'menu_postion' => 5,
           'public' =>true,
           'exclude_from_search' => true,
           'has_archives'=>false,
           'register_meta_box_cb' => 'metaField_metabox',
           'supports' => array('title', 'editor', 'thumbnail')
        )
    );
 }

  /** Add new custom fields to create post form*/
 function metaField_metabox() {
     add_meta_box( 'meta_fields', 'Meta Fields', 'display_metaFields', 'example','normal','high' );
 }

 function display_metaFields(){
     global $post;

     $meta_title = get_post_meta($post->ID,'meta_title', true);
     $meta_description = get_post_meta($post->ID,'meta_description', true);
    ?>
        <label>Meta Title</label>
        <input type="text" name="meta_title" placeholder="Meta Title" class="widefat" value="<?php print $meta_title?>">
        </br>
        </br>
        <label>Meta Description</label>
        <input type="text" name="meta_description" placeholder="Meta Description"class="widefat" value="<?php print $meta_description?>">
    <?php
 }
 add_action( 'add_meta_box','metaField_metabox' );

/** save custom field data to posts table */
function saveMetaFields($post_id){
    $is_autosave = wp_is_post_autosave($post_id );
    $is_revision = wp_is_post_revision( $post_id );

    if( $is_autosave || $is_revision){
        return;
    }

    $post = get_post($post_id);

    if ($post->post_type == "example") {
        if (array_key_exists('meta_title', $_POST)) {
            update_post_meta( $post_id, 'meta_title',$_POST['meta_title']);
        }

        if (array_key_exists('meta_description', $_POST)) {
            update_post_meta( $post_id, 'meta_description',$_POST['meta_description']);
        }        
    }
}
add_action( 'save_post','saveMetaFields' );

With the above code I'm successful to add custom field data in the posts table.

Now I want to place the meta_title and meta_description of a each post in the header section of a post. Like:

<meta name="title">....</title>
<meta name="description">....</title>

Upvotes: 1

Views: 226

Answers (1)

aidinMC
aidinMC

Reputation: 1426

Use wp_head action:

function myAction() {
    if(is_single('Example'):
        $postID = get_the_ID();
        $title = get_post_meta($postID,'meta_title',true);
        $desc = get_post_meta($postID,'meta_description',true);
  ?>
        <meta name="title" content="<?php echo $title; ?>" />
        <meta name="description" content="<?php echo $desc; ?>"/>

  <?php
    endif;
}
add_action( 'wp_head', 'myAction' );

Upvotes: 1

Related Questions