【问题标题】:Adding a Custom Checkbox to the Post “Publish” Box of wordpress将自定义复选框添加到 wordpress 的“发布”框
【发布时间】:2021-10-25 22:52:38
【问题描述】:

感谢我找到的一篇文章 online 我能够将自定义复选框添加到 Wordpress 的“发布”框。但是它确实提醒我一个错误并且我不知道如何去修复它,因为我仍然是 PHP 新手。

感谢您的帮助,如果您不介意在答案中包含更正的代码,我将非常感激!

///////////// 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');
    }
}

但这只会给我们一个错误:

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

【问题讨论】:

  • 您在 saveCustomField 函数之前缺少一个“>”。

标签: php wordpress function wordpress-theming


【解决方案1】:

您的 add_action 回调函数中缺少引号。这就是为什么它认为它是一个未定义的常量。

add_action('post_submitbox_misc_actions', 'createCheckBoxField');

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-15
    • 2016-08-12
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    相关资源
    最近更新 更多