add_featured_meta_box

Where would add_featured_meta_box function be added and how should I call it.

So far I have in extras.php

        // Create a Featured Article Function
        function register_post_assets(){
                add_meta_box('featured-post', __('Featured Post'), 'add_featured_meta_box', 'post', 'advanced', 'high');
        }
        add_action('admin_init', __NAMESPACE__ . '\\register_post_assets', 1);

        function add_featured_meta_box($post){
                $featured = get_post_meta($post->ID, '_featured-post', true);
                echo "<label for='_featured-post'>".__('Feature this post?', 'sage')."</label>";
                echo "<input type='checkbox' name='featured-post' id='featured-post' value='1' ".checked(1, $featured)." />";
        }

        function save_featured_meta($post_id){
             if (isset($_REQUEST['featured-post']))
                        update_post_meta(esc_attr($post_id), '_featured-post', esc_attr($_REQUEST['featured-post'])); 
                        }
        add_action('save_post', __NAMESPACE__ . '\\save_featured_meta');


But am getting an error function 'add_featured_meta_box' not found.

Can someone point me in the right direction, please?

You need to add the namespace when you reference the callback in register_post_assets:

function register_post_assets(){
    add_meta_box('featured-post', __('Featured Post'), __NAMESPACE__ . '\\add_featured_meta_box', 'post', 'advanced', 'high');
}
add_action('admin_init', __NAMESPACE__ . '\\register_post_assets', 1);
1 Like