Add Schema for Wordpress page

PHOTO EMBED

Wed Aug 07 2024 21:00:10 GMT+0000 (Coordinated Universal Time)

Saved by @wasim_mm1

function cst_schema_field(){
    add_meta_box(
        'cft_metabox_field',
        'Add Schema',
        'cft_callback_func',
        array('page'),
        'normal',
        'high'
        );
}

add_action('add_meta_boxes','cst_schema_field');

function cft_callback_func($post){
    echo '<style>
        .schema_cft {
            width: 100%;
        }
    </style>';
    
    $meta_value = get_post_meta($post->ID, '_schema_cft', true);
    
    echo '<textarea name="schema_cft" class="schema_cft" rows="15">' . $meta_value . '</textarea>';
}

function cft_save_func($post_id){
    
    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
    if(!current_user_can('administrator', $post_id)) return;
    
    if(isset($_POST['schema_cft'])){
        update_post_meta($post_id, '_schema_cft', $_POST['schema_cft']);
    }
}

add_action('save_post','cft_save_func');

function display_schema_cft_in_head() {
    if (is_singular('page')) {
        $page_id = get_queried_object_id();
        
        $schema_cft_value = get_post_meta($page_id, '_schema_cft', true);

        if (!empty($schema_cft_value)) {
            echo $schema_cft_value;
        }
    }
}
content_copyCOPY