自定义页面中的WordPress AJAX更新自定义字段
我正在使用WordPress使用AJAX和jQuery使用DropdownChangeEvent更新我的自定义字段,它可以按照我的要求正常工作。这是我的工作代码 my subscriptions.php页面自定义页面中的WordPress AJAX更新自定义字段,wordpress,Wordpress,我正在使用WordPress使用AJAX和jQuery使用DropdownChangeEvent更新我的自定义字段,它可以按照我的要求正常工作。这是我的工作代码 my subscriptions.php页面 <?php wp_enqueue_script( 'script', get_template_directory_uri() . '/mindesk-ecommerce/assets/js/custom.js', array ( 'jquery' ), 1.1, true); $
<?php
wp_enqueue_script( 'script', get_template_directory_uri() . '/mindesk-ecommerce/assets/js/custom.js', array ( 'jquery' ), 1.1, true);
$ajaxPageUrl = admin_url( 'admin-ajax.php');
?>
<select id="myid" class="mySubscription" name="mindesk_wc_subscriptions_var_client_user_id" data-action="http://localhost/wordpress/wp-admin/admin-ajax.php" data-subscription-id="635"><option value=""></option><option value="24">mittul</option><option value="10">subscriber</option></select>
functions.php
add_action( 'wp_ajax_nopriv_update_subscription_custom_meta', 'update_subscription_custom_meta' );
add_action( 'wp_ajax_update_subscription_custom_meta', 'update_subscription_custom_meta' );
function update_subscription_custom_meta() {
update_post_meta($_POST['subscriptionId'], 'my_custom_field', $_POST['userId']);
echo json_encode(array('status'=>1),true);
exit;
}
正如您在我使用的mysubscriptions.php文件中所看到的
$ajaxPageUrl = admin_url( 'admin-ajax.php');
基于custom.js文件中的mySubscription
类,我使用update\u subscription\u custom\u meta
作为action
,然后进入functions.php文件更新我的自定义元数据。这一切都很好
但是当我将$ajaxPageUrl
更改为如下内容时
$ajaxPageUrl = get_template_directory_uri() . '/mindesk-ecommerce/ajax/updatesubscription.php';
然后创建了文件updatesubscription.php
,并将我上面放在functions.php
文件中的相同代码放入,然后它就不工作了
它总是给我500个错误。。有人能指引我吗。。将functions.php的代码放在我的一个页面上是可能的还是理想的方式
我想以那种方式遵循结构
有人能指导我如何做到这一点吗
谢谢在function.php文件中包含
updatesubscription.php
include( get_template_directory() . '/mindesk-ecommerce/ajax/updatesubscription.php' );
以及在my-subscriptions.php文件中使用的ajaxPageUrl
$ajaxPageUrl = admin_url( 'admin-ajax.php');
你检查错误日志了吗?没有日志。。我想在自定义页面updatesubscription.php中使用与我在functions.php文件中为更新自定义字段添加的代码相同的代码。我不想使用functions.php。。而是将其中的代码添加到我创建的updatesubscription.php文件中。这是可能的还是正确的方法?在functions.php中,您添加了一个在updatesubscription.php中找不到的ajax操作,这就是为什么会出现错误的原因。是。。我明白这一点。那我该怎么办呢?为什么你想从
updatesubscription.php
Hi Bhautik调用ajax,你能帮我一下吗。。我已经向你展示了我在那里的尝试。谢谢
$ajaxPageUrl = admin_url( 'admin-ajax.php');