使用AJAX的WordPress自定义元数据输入值

我正在使用Wordpress 3.5,我有一个带有元数据和一些输入字段的自定义post(sp_product)。 其中一个输入(sp_title)。

我想通过输入我的输入(sp_title)字段按自定义post标题名称搜索,当我按下添加按钮(也在我的自定义元框中)时,它会找到该post的标题名称并带来一些post元数据进入这个Meta框并显示到其他字段中。

在此处输入图像描述

在这张图片中(例子)

  1. 搜索
  2. 单击按钮
  3. 从自定义post中获取AJAX的一些价值。

请给我一个示例代码(只是简单)

  1. 我将搜索一个简单的自定义post标题,
  2. 单击一个按钮
  3. 获取该post的标题(我搜索或匹配)与任何其他后元值,通过AJAX(jQuery-AJAX)。

请帮我。

我找到了领先优势,因为我的一个插件使用类似于重新附加图像的东西。
因此,相关的Javascript函数是findPosts.open('action','find_posts')

它似乎没有很好的记录,我只能找到两篇关于它的文章:

  • 查找post对话框
  • 在插件中使用内置Post Finder

试图实现两个代码示例,模态窗口打开但转储-1错误。 那是因为Ajax调用没有在函数wp_ajax_find_posts传递wp_ajax_find_posts

因此,以下工作,它基于第二篇文章。 但它有一个必须解决的安全漏洞,即wp_nonce_field – > check_ajax_referer 。 它在代码注释中指出。
要打开“post选择器”,请双击文本字段。
需要解决jQuery Select问题。

插件文件

 add_action( 'load-post.php', 'enqueue_scripts_so_14416409' ); add_action( 'add_meta_boxes', 'add_custom_box_so_14416409' ); add_action( 'wp_ajax_find_posts', 'replace_default_ajax_so_14416409', 1 ); /* Scripts */ function enqueue_scripts_so_14416409() { # Enqueue scripts wp_enqueue_script( 'open-posts-scripts', plugins_url('open-posts.js', __FILE__), array('media', 'wp-ajax-response'), '0.1', true ); # Add the finder dialog box add_action( 'admin_footer', 'find_posts_div', 99 ); } /* Meta box create */ function add_custom_box_so_14416409() { add_meta_box( 'sectionid_so_14416409', __( 'Select a Post' ), 'inner_custom_box_so_14416409', 'post' ); } /* Meta box content */ function inner_custom_box_so_14416409( $post ) { ?> 
true ), 'objects' ); unset( $post_types['attachment'] ); $s = stripslashes( $_POST['ps'] ); $searchand = $search = ''; $args = array( 'post_type' => array_keys( $post_types ), 'post_status' => 'any', 'posts_per_page' => 50, ); if ( '' !== $s ) $args['s'] = $s; $posts = get_posts( $args ); if ( ! $posts ) wp_die( __('No items found.') ); $html = ''; foreach ( $posts as $post ) { $title = trim( $post->post_title ) ? $post->post_title : __( '(no title)' ); switch ( $post->post_status ) { case 'publish' : case 'private' : $stat = __('Published'); break; case 'future' : $stat = __('Scheduled'); break; case 'pending' : $stat = __('Pending Review'); break; case 'draft' : $stat = __('Draft'); break; } if ( '0000-00-00 00:00:00' == $post->post_date ) { $time = ''; } else { /* translators: date format in table columns, see http://php.net/date */ $time = mysql2date(__('Y/m/d'), $post->post_date); } $html .= ''; $html .= '' . "\n\n"; } $html .= '

'.__('Title').''.__('Type').''.__('Date').''.__('Status').'
' . esc_html( $post_types[$post->post_type]->labels->singular_name ) . ''.esc_html( $time ) . '' . esc_html( $stat ). '
'; $x = new WP_Ajax_Response(); $x->add( array( 'data' => $html )); $x->send(); }

Javascript文件open-posts.js

 jQuery(document).ready(function($) { // Find posts var $findBox = $('#find-posts'), $found = $('#find-posts-response'), $findBoxSubmit = $('#find-posts-submit'); // Open $('input.kc-find-post').live('dblclick', function() { $findBox.data('kcTarget', $(this)); findPosts.open(); }); // Insert $findBoxSubmit.click(function(e) { e.preventDefault(); // Be nice! if ( !$findBox.data('kcTarget') ) return; var $selected = $found.find('input:checked'); if ( !$selected.length ) return false; var $target = $findBox.data('kcTarget'), current = $target.val(), current = current === '' ? [] : current.split(','), newID = $selected.val(); if ( $.inArray(newID, current) < 0 ) { current.push(newID); $target.val( current.join(',') ); } }); // Double click on the radios $('input[name="found_post_id"]', $findBox).live('dblclick', function() { $findBoxSubmit.trigger('click'); }); // Close $( '#find-posts-close' ).click(function() { $findBox.removeData('kcTarget'); }); });