Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用AJAX将php函数添加到javascript文件中(与tinymce和wordpress相关)_Php_Jquery_Ajax_Wordpress - Fatal编程技术网

使用AJAX将php函数添加到javascript文件中(与tinymce和wordpress相关)

使用AJAX将php函数添加到javascript文件中(与tinymce和wordpress相关),php,jquery,ajax,wordpress,Php,Jquery,Ajax,Wordpress,我正在为wp WYSIWYG编辑器创建一个tinyMCE按钮。基本上,当用户点击按钮时,会弹出一个模式表单,他们必须输入几个字段。但是,其中一个字段需要是一个列表框,列出每个帖子类别,用户将选择一个。其基本语法如下所示: { type: 'listbox', name: 'sds-category', label: 'Category', 'values': [ {text: 'Name Of Cat', value: 'Cat ID'}, {text: 'Name Of

我正在为wp WYSIWYG编辑器创建一个tinyMCE按钮。基本上,当用户点击按钮时,会弹出一个模式表单,他们必须输入几个字段。但是,其中一个字段需要是一个列表框,列出每个帖子类别,用户将选择一个。其基本语法如下所示:

{
type: 'listbox', 
name: 'sds-category', 
label: 'Category', 
'values': [
    {text: 'Name Of Cat', value: 'Cat ID'},
    {text: 'Name Of Cat', value: 'Cat ID'},
    {text: 'Name Of Cat', value: 'Cat ID'}]}
因此,为了让所有类别都像这样显示,我使用了一个PHPfunction,它将为每个类别输出
{text:'',value:''}
语法,如下所示:

function write_cat_list($cat){
    $cats = get_categories('hide_empty=false&orderby=name&order=ASC&parent=' . $cat);

    if($cats) :
        foreach ($cats as $cat) :
            $tinyMCE_list[] = "{text: '".$cat->name."', value: '".$cat->term_id."'}";
            write_cat_list($cat->term_id);
        endforeach;
        echo implode(',', $tinyMCE_list);
    endif;
}
现在剩下的就是把PHP函数
write_cat_list(0)
放到我的.js文件中,这就是我完全卡住的地方


我不知道该怎么做,因为我对AJAX非常缺乏经验,有没有一种简单的方法或jquery函数可以让我轻松地将php函数包含到这个js文件中

因为您是递归收集类别,所以在foreach循环中打印值,或者将值收集到数组中(就像您正在做的那样),然后进一步传递该值。以下是一个例子:

function list_categories( $cat_id, &$output = array() )
{
    $categories = get_categories( 'hide_empty=0&orderby=name&order=ASC&parent=' . $cat_id );

    foreach( $categories as $cat ) {
        $output[] = array( 'text' => $cat->name, 'value' => $cat->term_id );
        list_categories( $cat->term_id, $output );
    }
    return $output;
}

$list = list_categories(0); // to get an array of categories
有几种方法可以将输出包含到.js文件中。如果脚本是使用.php文件生成的,则使用:

'values': <?php echo json_encode( list_categories(0) ); ?>
另一个选项是直接在管理头中打印值:

add_action( 'admin_enqueue_scripts', 'mce_admin_scripts' );
function mce_admin_scripts( $hook ) {
    if ( $hook == 'post.php' || $hook == 'post-new.php' ) {
        add_action( "admin_head-$hook", 'mce_admin_head' );
    }
}
function mce_admin_head() {
    echo '<script type="text/javascript">var mce_options=' . json_encode( array( 'categories' => list_categories(0) ) ) . '; </script>';
}
add_action('admin_enqueue_scripts'、'mce_admin_scripts');
函数mce_管理_脚本($hook){
如果($hook=='post.php'| |$hook=='post new.php'){
添加动作(“管理头-$hook”,“mce管理头”);
}
}
职能mce_admin_head(){
echo'var mce_options='.json_encode(数组('categories'=>list_categories(0)));
}
add_action( 'admin_enqueue_scripts', 'mce_admin_scripts' );
function mce_admin_scripts( $hook ) {
    if ( $hook == 'post.php' || $hook == 'post-new.php' ) {
        add_action( "admin_head-$hook", 'mce_admin_head' );
    }
}
function mce_admin_head() {
    echo '<script type="text/javascript">var mce_options=' . json_encode( array( 'categories' => list_categories(0) ) ) . '; </script>';
}