使用AJAX的WordPress自定义元数据输入值
我正在使用Wordpress 3.5,我有一个带有元数据和一些输入字段的自定义帖子(sp_product).其中一个输入(sp_title).
我想通过输入我的输入(sp_title)字段按自定义帖子标题名称搜索,当我按下添加按钮(也在我的自定义元框中)时,它会找到该帖子的标题名称并带来一些帖子元数据进入这个Meta框并显示到其他字段中. 在这张图片中(例子) >搜索 请给我一个示例代码(只是简单) >我将搜索一个简单的自定义帖子标题, 请帮我. 解决方法
我能够找到领先优势,因为我的一个插件使用类似于重新附加图像的东西.
因此,相关的 Javascript函数是findPosts.open(‘action’,’find_posts’). 它似乎没有很好的记录,我只能找到两篇关于它的文章: > Find Posts Dialog Box 试图实现两个代码示例,模态窗口打开但转储-1错误.那是因为Ajax调用没有在函数 因此,以下工作,它基于第二篇文章.但它有一个必须解决的安全漏洞,即wp_nonce_field – > check_ajax_referer.它在代码注释中指出. 插件文件 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 ) { ?> <form id="emc2pdc_form" method="post" action=""> <?php wp_nonce_field( 'find-posts','_ajax_nonce',false); ?> <input type="text" name="kc-find-post" id="kc-find-post" class="kc-find-post"> </form> <?php } /* Ajax replacement - Verbatim copy from wp_ajax_find_posts() */ function replace_default_ajax_so_14416409() { global $wpdb; // SECURITY BREACH // check_ajax_referer( '_ajax_nonce' ); $post_types = get_post_types( array( 'public' => 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 = '<table class="widefat" cellspacing="0"><thead><tr><th class="found-radio"><br /></th><th>'.__('Title').'</th><th class="no-break">'.__('Type').'</th><th class="no-break">'.__('Date').'</th><th class="no-break">'.__('Status').'</th></tr></thead><tbody>'; 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 .= '<tr class="found-posts"><td class="found-radio"><input type="radio" id="found-'.$post->ID.'" name="found_post_id" value="' . esc_attr($post->ID) . '"></td>'; $html .= '<td><label for="found-'.$post->ID.'">' . esc_html( $title ) . '</label></td><td class="no-break">' . esc_html( $post_types[$post->post_type]->labels->singular_name ) . '</td><td class="no-break">'.esc_html( $time ) . '</td><td class="no-break">' . esc_html( $stat ). ' </td></tr>' . "nn"; } $html .= '</tbody></table>'; $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'); }); }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |