通过编程将选项添加到“添加新的”自定义字段下拉菜单

时间:2013-05-04 作者:mhulse

如何将预定义选项添加到“添加新”自定义字段下拉列表中?

enter image description here

以下是两个自动添加和显示新自定义字段的示例:

  1. WordPress: Adding Default Custom Fields on New Posts
  2. Auto create custom field

    我想知道如何在不使用插件的情况下做到这一点。

4 个回复
最合适的回答,由SO网友:fuxia 整理而成

纯PHP无法做到这一点,因为字段是从现有字段中提取的,并且没有挂钩。但您可以使用JavaScript,检查帖子类型是否支持自定义字段,并且该字段是否不存在,然后插入它:

<?php # -*- coding: utf-8 -*-
/* Plugin Name: Extend custom fields */

add_action( \'admin_footer-post-new.php\', \'wpse_98269_script\' );
add_action( \'admin_footer-post.php\', \'wpse_98269_script\' );

function wpse_98269_script()
{
    if ( ! isset ( $GLOBALS[\'post\'] ) )
        return;

    $post_type = get_post_type( $GLOBALS[\'post\'] );

    if ( ! post_type_supports( $post_type, \'custom-fields\' ) )
        return;
    ?>
<script>
    if ( jQuery( "[value=\'demo_data\']" ).length < 1 ) // avoid duplication
        jQuery( "#metakeyselect").append( "<option value=\'demo_data\'>demo_data</option>" );
</script>
    <?php
}

SO网友:CyberXoft

function add_predefined_custom_field_names( $query ) {
    $predefined = array(
        \'www.cyberxoft.com\'
    );

    global $table_prefix;

    $query = preg_replace(\'/[\\r\\n\\t]/\', \' \', $query); //minify by removing all tabs and line breaks
    $query = preg_replace(\'/\\s+/\', \' \', $query); //minify by replacing spaces, tabs and carriages to single space

    //SELECT meta_key FROM wp_postmeta GROUP BY meta_key HAVING meta_key NOT LIKE \'\\\\_%\' ORDER BY meta_key LIMIT 30
    $pattern = ("/SELECT meta_key FROM ".$table_prefix."postmeta/i");   

    if( preg_match($pattern, $query) ) {
        $keys = \'\';     

        foreach($predefined as $key){$keys .= (" UNION SELECT \'$key\' AS meta_key");}        

        $query = preg_replace(\'/SELECT/i\', \'SELECT meta_key FROM (SELECT\', $query);
        $query = preg_replace(\'/FROM wp_postmeta/i\', (\'FROM wp_postmeta\'.$keys), $query);
        $query = preg_replace(\'/ GROUP BY/i\', \')t GROUP BY\', $query);
    }

    return $query;
}
add_filter(\'query\', \'add_predefined_custom_field_names\');
只需在主题函数的任何位置添加上述代码。php。添加上述代码后,它会添加“www.cyberxoft”。com”作为要选择的选项之一,添加到下拉列表中。

如果你能看到它,那么只需替换“www.cyberxoft”。使用您所需的字段名进行com,并刷新管理页面,当您看到发生这种情况时,请继续添加任意数量的内容,但请记住,只能查看30个,因为这是为其设置的默认限制。

享受

SO网友:mhulse

以下是awesome @toscho发布的脚本。我只需要能够创建<select> 如果它不存在的话。

/**
 * Programatically add custom fields.
 *
 * @see http://wordpress.stackexchange.com/questions/98269/programatically-add-options-to-add-new-custom-field-dropdown/
 */

function wpse_98269_script() {

    if (isset($GLOBALS[\'post\'])) {

        $post_type = get_post_type($GLOBALS[\'post\']);

        if (post_type_supports($post_type, \'custom-fields\')) {

            ?>

                <script>

                    // Cache:
                    var $metakeyinput = jQuery(\'#metakeyinput\'),
                        $metakeyselect = jQuery(\'#metakeyselect\');

                    // Does the default input field exist and is it visible?
                    if ($metakeyinput.length && ( ! $metakeyinput.hasClass(\'hide-if-js\'))) {

                        // Hide it:
                        $metakeyinput.addClass(\'hide-if-js\'); // Using WP admin class.

                        // ... and create the select box:
                        $metakeyselect = jQuery(\'<select id="metakeyselect" name="metakeyselect">\').appendTo(\'#newmetaleft\');

                        // Add the default select value:
                        $metakeyselect.append(\'<option value="#NONE#">— Select —</option>\');

                    }

                    // Does "demo_data" already exist?
                    if (jQuery("[value=\'demo_data\']").length < 1) {

                        // Add option:
                        $metakeyselect.append("<option value=\'demo_data\'>demo_data</option>");

                    }

                </script>

            <?php

        }

    }

}

add_action(\'admin_footer-post-new.php\', \'wpse_98269_script\');
add_action(\'admin_footer-post.php\', \'wpse_98269_script\');
我相信我的JS改编可以改进,但它完成了工作。如果我做了更改/改进,我会将更新的代码发布回这里。

再次感谢@toscho!!!!我欠你一个人情。:)

SO网友:Mohammad Yousefi

它在wordpress v5中工作。8.1

add_filter( \'postmeta_form_keys\', \'filter_function_name_1552\', 10, 2 );
function filter_function_name_1552( $keys, $post ){
    if($post->post_type == \'car\') {
        $keys[] = \'regular_price\';
    }
    return $keys;
}

结束

相关推荐

如何在Functions.php中使用PHP手动修复WordPress库代码?

Wordpress为内置的gallery功能输出了一些非常糟糕的代码,这已经被谈论了很多次了。这是负责库输出的核心代码(在/wp-includes/media.php中):function gallery_shortcode($attr) { global $post; static $instance = 0; $instance++; // Allow plugins/themes to override the de