How to add a field in edit post page inside Publish box in Wordpress?

后端 未结 4 1487
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 06:01

I want to add a new checkbox field inside Publish block in add/edit post page. Does anyone have idea how to do that ?

4条回答
  •  温柔的废话
    2021-01-30 06:29

    rbncha's code didn't work out of the box and needed a lot of tweaking, the code below is what I came up with. I've added some comments which explains everything thoroughly.

    The following code adds a checkbox in the publish block of posts (you can easily change the post type), and stores/retrieves the value in/from the database. With some minor tweaking you could easily add a text field or anything you like.

    It should be noted that you have to change my_ to a unique key for your theme or plugin!

    add_action( 'post_submitbox_misc_actions', 'my_featured_post_field' );
    function my_featured_post_field()
    {
        global $post;
    
        /* check if this is a post, if not then we won't add the custom field */
        /* change this post type to any type you want to add the custom field to */
        if (get_post_type($post) != 'post') return false;
    
        /* get the value corrent value of the custom field */
        $value = get_post_meta($post->ID, 'my_featured_post_field', true);
        ?>
            

    If you want to read more about custom fields see here: http://codex.wordpress.org/Custom_Fields

提交回复
热议问题