Php 如何在Wordpress的“发布”框中的“编辑文章”页面中添加字段?

Php 如何在Wordpress的“发布”框中的“编辑文章”页面中添加字段?,php,wordpress,Php,Wordpress,我想在“添加/编辑帖子”页面的“发布”块中添加一个新的复选框字段。有人知道怎么做吗?使用wordpress的插件 好吧!,我找不到在发布块中添加字段的解决方案。对于临时解决方案,我通过简单地添加如下简单代码添加了新块 添加操作('admin_init','category_metabox') //将新发布添加到frontpage框 添加元框( “在frontpage中发布”, “在Frontpage中发布”, “在frontpage中发布”, "文章",, "一边",, “高” ); 函数发布

我想在“添加/编辑帖子”页面的“发布”块中添加一个新的复选框字段。有人知道怎么做吗?

使用wordpress的插件

好吧!,我找不到在发布块中添加字段的解决方案。对于临时解决方案,我通过简单地添加如下简单代码添加了新块


添加操作('admin_init','category_metabox')

//将新发布添加到frontpage框
添加元框(
“在frontpage中发布”,
“在Frontpage中发布”,
“在frontpage中发布”,
"文章",,
"一边",,
“高”
);
函数发布在frontpage回调中($post)
{
$value=get_post_meta($post->ID,'u publish_in_frontpage',true);
echo“发布到frontpage”;
}
添加操作('save_post','save_postdata');
函数save_postdata($posted)
{   
if(defined('DOING_AUTOSAVE')&&DOING_AUTOSAVE)返回false;
如果(!current_user_can('edit_page',$postid))返回false;
if(empty($posted)|$\u POST['POST\u type']!='article')返回false;
如果($_POST['action']=='editpost'){
删除帖子元($posted,'publish_in_frontpage');
}
添加帖子元($posted,'publish_in_frontpage',$_post['publish_in_frontpage']);
}

我终于找到了解决方案。我希望它对某人有好处

add_action( 'post_submitbox_misc_actions', 'publish_in_frontpage' );
function publish_in_frontpage($post)
{
    $value = get_post_meta($post->ID, '_publish_in_frontpage', true);
    echo '<div class="misc-pub-section misc-pub-section-last">
         <span id="timestamp">'
         . '<label><input type="checkbox"' . (!empty($value) ? ' checked="checked" ' : null) . 'value="1" name="publish_in_frontpage" /> Publish to frontpage</label>'
    .'</span></div>';
}

function save_postdata($postid)
{   
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
    if ( !current_user_can( 'edit_page', $postid ) ) return false;
    if(empty($postid) || $_POST['post_type'] != 'article' ) return false;

    if($_POST['action'] == 'editpost'){
        delete_post_meta($postid, 'publish_in_frontpage');
    }

    add_post_meta($postid, 'publish_in_frontpage', $_POST['publish_in_frontpage']);
}
add_action('post_submitbox_misc_actions','publish_in_frontpage');
函数在frontpage中发布($post)
{
$value=get_post_meta($post->ID,'u publish_in_frontpage',true);
回声'
'
“发布到frontpage”
.'';
}
函数save_postdata($posted)
{   
if(defined('DOING_AUTOSAVE')&&DOING_AUTOSAVE)返回false;
如果(!current_user_can('edit_page',$postid))返回false;
if(empty($posted)|$\u POST['POST\u type']!='article')返回false;
如果($_POST['action']=='editpost'){
删除帖子元($posted,'publish_in_frontpage');
}
添加帖子元($posted,'publish_in_frontpage',$_post['publish_in_frontpage']);
}

rbncha的代码不是现成的,需要进行大量调整,下面的代码就是我想到的。我添加了一些注释,对所有内容都做了透彻的解释

下面的代码在post的publish块中添加了一个复选框(您可以轻松更改post类型),并在数据库中存储/检索值。通过一些细微的调整,您可以轻松添加文本字段或任何您喜欢的内容

需要注意的是,您必须将主题或插件的
my\ucode>更改为唯一键

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);
    ?>
        <div class="misc-pub-section">
            <?php //if there is a value (1), check the checkbox ?>
            <label><input type="checkbox"<?php echo (!empty($value) ? ' checked="checked"' : null) ?> value="1" name="my_featured_post_field" /> Featured on frontpage</label>
        </div>
    <?php
}

add_action( 'save_post', 'my_save_postdata');
function my_save_postdata($postid)
{
    /* check if this is an autosave */
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;

    /* check if the user can edit this page */
    if ( !current_user_can( 'edit_page', $postid ) ) return false;

    /* check if there's a post id and check if this is a post */
    /* make sure this is the same post type as above */
    if(empty($postid) || $_POST['post_type'] != 'post' ) return false;

    /* if you are going to use text fields, then you should change the part below */
    /* use add_post_meta, update_post_meta and delete_post_meta, to control the stored value */

    /* check if the custom field is submitted (checkboxes that aren't marked, aren't submitted) */
    if(isset($_POST['my_featured_post_field'])){
        /* store the value in the database */
        add_post_meta($postid, 'my_featured_post_field', 1, true );
    }
    else{
        /* not marked? delete the value in the database */
        delete_post_meta($postid, 'my_featured_post_field');
    }
}
add_action('post_submitbox_misc_actions'、'my_featured_post_field');
函数my_featured_post_字段()
{
全球$员额;
/*检查这是否是一篇文章,如果不是,我们将不添加自定义字段*/
/*将此帖子类型更改为要将自定义字段添加到的任何类型*/
if(get_post_type($post)!=“post”)返回false;
/*获取自定义字段的相应值*/
$value=get_post_meta($post->ID,'my_featured_post_field',true);
?>

选中此->我已检查了更多字段。我们是否可以通过其他方式(如add_meta_box())添加一个新字段?我找不到在高级自定义字段的Publish块中添加复选框的方法。我从未想过在该框中添加一些内容,但有(某种程度上没有文档记录)这样做的方法很酷。将此添加为星号供以后使用!只是一个小更新:
publish_in_frontpage()
似乎没有参数,所以我在里面使用了
global$post;
,效果很好。这段代码远远不完整/正确,而且不起作用,我怀疑它是否起作用。我在下面添加了我的固定/注释代码。它在最新的WP5中仍然有效吗?它看起来像do_操作(“post_submitbox_misc_actions”)从未在我的应用程序上触发。
检查自定义字段是否已提交
您可以在复选框之前添加隐藏字段,并使用相同的名称和值=0。这似乎是一种不好的做法?黑客解决方案?没有-许多框架都会这样做。因此,这可以称为现有做法。
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);
    ?>
        <div class="misc-pub-section">
            <?php //if there is a value (1), check the checkbox ?>
            <label><input type="checkbox"<?php echo (!empty($value) ? ' checked="checked"' : null) ?> value="1" name="my_featured_post_field" /> Featured on frontpage</label>
        </div>
    <?php
}

add_action( 'save_post', 'my_save_postdata');
function my_save_postdata($postid)
{
    /* check if this is an autosave */
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;

    /* check if the user can edit this page */
    if ( !current_user_can( 'edit_page', $postid ) ) return false;

    /* check if there's a post id and check if this is a post */
    /* make sure this is the same post type as above */
    if(empty($postid) || $_POST['post_type'] != 'post' ) return false;

    /* if you are going to use text fields, then you should change the part below */
    /* use add_post_meta, update_post_meta and delete_post_meta, to control the stored value */

    /* check if the custom field is submitted (checkboxes that aren't marked, aren't submitted) */
    if(isset($_POST['my_featured_post_field'])){
        /* store the value in the database */
        add_post_meta($postid, 'my_featured_post_field', 1, true );
    }
    else{
        /* not marked? delete the value in the database */
        delete_post_meta($postid, 'my_featured_post_field');
    }
}