简体   繁体   English

在wordpress的发布“发布”框中添加自定义复选框

[英]Adding a Custom Checkbox to the Post “Publish” Box of wordpress

Thanks to an article I found online I was able to add a Custom Checkbox to the Post “Publish” Box of Wordpress.感谢我在网上找到的一篇文章,我能够将自定义复选框添加到 Wordpress 的“发布”框。 However it does alert me of an error and am at a loss how to go about fixing it since I am still green in PHP.然而,它确实提醒我一个错误,并且我不知道如何修复它,因为我在 PHP 中仍然是绿色的。

Thanks for any help and if you don't mind including the corrected code inside your answer I will be very grateful!感谢您的帮助,如果您不介意在答案中包含更正的代码,我将不胜感激!

///////////// ADD WATERMARK CHECK BOX TO TOOLS POST ////////////////
add_action('post_submitbox_misc_actions', createCheckBoxField);
add_action('save_post', saveCustomField);

function createCheckBoxField()
{
    $post_id = get_the_ID();

    if (get_post_type($post_id) != 'tools' ) {
        return;
    }

    $value = get_post_meta($post_id, 'watermark_chk-box', true);
    wp_nonce_field('my_custom_nonce_'.$post_id, 'my_custom_nonce');
    ?>
    <div class="misc-pub-section misc-pub-section-last">
        <label><input type="checkbox" value="1" <?php checked($value, true, true); ?> name="watermark_chk-box" /><?php _e('Watermark Images', 'pmg'); ?></label>
    </div>
    <?php
}

function saveCustomField($post_id)
{
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }

    if (
        !isset($_POST['my_custom_nonce']) ||
        !wp_verify_nonce($_POST['my_custom_nonce'], 'my_custom_nonce_'.$post_id)
    ) {
        return;
    }

    if (!current_user_can('edit_post', $post_id)) {
        return;
    }
    if (isset($_POST['watermark_check-box'])) {
        update_post_meta($post_id, 'watermark_chk-box', $_POST['watermark_chk-box']);
    } else {
        delete_post_meta($post_id, 'watermark_chk-box');
    }
}

But this just throws us an error of:但这只会给我们带来一个错误:

Warning: Use of undefined constant createCheckBoxField - assumed 'createCheckBoxField' (this will throw an Error in a future version of PHP)

在此处输入图片说明

You're missing the quotes in your add_action functions for the callback.您在回调的add_action函数中缺少引号。 That's why it thinks it is an undefined constant.这就是为什么它认为它是一个未定义的常量。

add_action('post_submitbox_misc_actions', 'createCheckBoxField');

Ref:https://developer.wordpress.org/reference/functions/add_action/参考:https ://developer.wordpress.org/reference/functions/add_action/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM