javascript - 单击 WordPress 中的按钮后调用函数(开发插件)

标签 javascript jquery ajax wordpress

这是我的代码,我想做第 73 行附近注释的事情(//我如何在这里调用函数 post_type_search_callback???)。结果需要位于同一页面的“表单”下方。我正在网上尝试一些关于ajax的教程,但没有成功。请帮忙!谢谢!

<?php

class OwnersTriplify {

	const plugin_name = 'Owners-triplify';

	public static function head() {

	}

	public static function initialize() {

		add_action('admin_menu', 'owners_triplify_admin_actions');

		function owners_triplify_admin_actions() {
		
		add_menu_page('Owners Triplify', 'Owners Triplify', 'manage_options', 'owners-triplify/includes/OwnersTriplify.php', 'owners_triplify', plugins_url('owners-triplify/images/icon.png'));
		
		}

		function register_my_setting() {
		
		register_setting( 'hd-group', 'hd_options'); 
	
		}

		add_action( 'admin_init', 'register_my_setting' );

		add_action( 'admin_enqueue_scripts', 'post_type_search_enqueue' );

		function post_type_search_enqueue($hook) {
    			
			if( 'index.php' != $hook ) {
	
				// Only applies to dashboard panel
				return;
    			}
        
			wp_enqueue_script( 'ajax-script', plugins_url( '/js/post_type_search_query.js', __FILE__ ), array('jquery') );
			
			// in JavaScript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value
			
			wp_localize_script( 'ajax-script', 'ajax_object',
            
			array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );
		}

		function owners_triplify() {

			?>

			<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
			<script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script>

			<div id="div1">

				<h3>Digite o post-type que deseja triplificar: </h3>
				<br/>
					<form method="post" name="form1" action="">
						<input name="postType" value="" id="postType">
						<input type="hidden" name="action" value="update">
						<input type="hidden" name="option_page" value="hd_options">
						<button id="button1" name="termoPesquisado" type="submit" class="button-primary">Pesquisar</button>
					</form>	
				<br/>

			</div>

			<script>

				$("#button1").click(function () {

					//HOW CAN I CALL FUNCTION post_type_search_callback HERE???

				});

				/*$("#button1").click(function () {

					if ( $("#div2").is( ":hidden" ) ) {
						
						$("#div2").slideDown();
						
						$("#div1").hide();

						} 

						else {
						
						}

				});*/

			</script>

		<?php

		}

		function pegaValores($data) {

			$data = trim($data);
			$data = stripslashes($data);
			$data = htmlspecialchars($data);
			return $data;

		}

		$termo = pegaValores($_POST["postType"]);

		?>

		<?php

		add_action('wp_ajax_post_type_search', 'post_type_search_callback');

		function post_type_search_callback() {

			?>

			<div id="div2">

			<h3>Digite as equivalências: </h3>

			<?php

			global $wpdb;
			$resultado = $wpdb->get_results("SELECT distinct meta_key FROM $wpdb->postmeta WHERE post_id in(SELECT ID FROM $wpdb->posts WHERE post_type = '".$termo."')");
			$correspondencias;
			$tamanhoResultado = count($resultado);
			
			for($i = 1; $i <= $tamanhoResultado; $i++) {

				$correspondencias[$i] = "correspondencia";

			}
			
			$contador = 1;

			//$resultado_array = array();

			foreach($resultado as $resultadoX) {

				echo "<p>". 
				$resultado_array[] = $contador."- ".$resultadoX->meta_key." => ".
				"<input value=".$correspondencias[$contador]." id='correspondencia".$contador."'/>".
				"</p>"; //descobrir como colocar $contador + 1 para imprimir, a fim de que inicialize com 0 o contador e o for do correspondencias
				
				$contador++;

				//$datacount = implode('-',$resultado_array);

				?>

			<?php

			}

			?>

			<button id="button2" name="triplify" onclick="atualizaValores()" class="button-primary" >Triplificar</button>

			</div>

		<?php

		}


	}

}

?>

最佳答案

php函数

请注意,您正在初始化中声明所有函数......最好在其他函数之外声明函数并在需要时调用它们。

add_action( 'wp_ajax_post_type_search_callback', array( 'OwnersTriplify', 'my_action_post_type_search_callback' ) );
add_action( 'wp_ajax_post_type_search_callback', array( 'OwnersTriplify', 'my_action_post_type_search_callback' ) );

function post_type_search_callback() {
    $data= $_POST['variable'];

    $output= 'i was returned with ajax';
    //need to echo output and exit here
echo $output;
exit();
}

jQuery 您需要使用 jQuery 一词或定义 $ 以使 jQuery 在 WordPress 中工作。

jQuery(document).ready(function() { // wait for page to finish loading 
   jQuery("#button1").click(function () {

    jQuery.ajax({
        type: "POST",
        url: "/wp-admin/admin-ajax.php",
        data: {
            action: 'post_type_search_callback',
            variable: 45 // enter in anyname here instead of variable, you will need to catch this value using $_POST['variable'] in your php function.
        },
        success: function (output) {
           console.log(output);
        }
    });

  });
});

进一步阅读:

http://web-profile.com.ua/wordpress/dev/ajax-in-wordpress/

关于javascript - 单击 WordPress 中的按钮后调用函数(开发插件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26266661/

相关文章:

Javascript点击iframe内的坐标

javascript - 将焦点设置到 jQuery UI MultiSelect 小部件中的过滤器输入

javascript - 使用 jQuery 和 HTML 动态创建标签并使用 jQuery 访问其文本值

javascript - 将 $ajax 转换为 fetch() 以进行 PUT 请求

javascript - 将项目从数组前面添加到数组末尾

javascript - firebase.storage() 不带参数或使用 Firebase App 实例

jquery - 如何使用jquery加载jquery对话框后在下拉列表中设置一些文本

jquery - Firefox SSL 和 JQuery 用户界面

javascript - 如何使用 jquery 将 &lt;input type ="text"> 更改为 <tr>

jquery - 使用 jQuery ajax 响应 html 更新 div