提前感谢所有花时间回复的人。So here is what I am trying to achieve: 本质上,我试图创建一个插件,从插件管理页面编辑我页面上的所有元描述。
我使用以下逻辑来实现这一点-在一个页面上打印数据库中的所有元。使用jQuery AJAX从所述页面获取所有页面ID和值。提交时-将所有更新的值推送到数据库中各自的位置,并更新这些元描述。
我不确定我是否在概念层面上有缺陷的代码,或者只是缺少一些基本的东西,但下面是我编写的代码。我试过阅读和测试我能做的一切,但都没有用。任何启示都是非常有益的。
请注意,我的回调函数有三个foreach循环,它们都不起作用,但我留下了它们,以便您可以看到我尝试过的一些东西。
再次感谢!
<!-- Page Creation -->
<?php
function ews_meta_page_creation_stephen() { ?>
<div>
<form class="metas-plugin" method="post">
<?php display_ews_metas_ids(); ?>
<?php submit_button();?>
</form>
</div>
<?php } ?>
<!-- jQuery & AJAX Function -->
<script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
<script>
jQuery(document).ready(function() {
// On form submission - run the function
jQuery("input#submit").click(function(event) {
var postMeta = [];
var postID = [];
// For each input - push the values to their respective empty arrays
jQuery(".descinput").each(function() {
postMeta.push(jQuery(this).val());
postID.push(jQuery(this).attr("name"));
});
// Once completed - json-ify the values for posting to PHP
var finalPostMeta = JSON.stringify(postMeta);
var finalPostID = JSON.stringify(postID);
// Send the arrays using POST in an AJAX Request
jQuery.ajax({
type: "POST",
url: \'https://dev.ewsproduction.com/dev6/ewstheme/wp-admin/admin-ajax.php\',
dataType:\'json\',
data: {
action: "update_meta_descriptions",
post_ids: finalPostID,
metas: finalPostMeta,
},
// Just for testing to make sure the values that are being sent are correct
success: function( data ) {
console.log(\'Success!\');
console.log(finalPostMeta);
console.log(postMeta);
console.log(finalPostID);
console.log(postID);
}
});
});
});
</script>
<!-- Callback AJAX function used to update the respective meta descriptions -->
<?php
add_action( \'wp_ajax_update_meta_descriptions\', \'update_meta_descriptions\' );
// AJAX Callback function to process the POST Data
function update_meta_descriptions() {
// Should Return Meta Description Array in a JSON Format
$metas = json_decode(stripslashes($_POST[\'finalPostMeta\']));
// Should Return Post ID Array in a JSON Format
$post_ids = json_decode(stripslashes($_POST[\'finalPostID\']));
// None of the below for each loops worked. What I am trying to achieve is using the post id - update the post meta for the respective post with whatever value was added before submit.
// Did not work but an example of what I tried
foreach ($post_ids as $key=> $meta) {
update_post_meta( $post_id, \'_metadescs\', $meta);
}
// Did not work but an example of what I tried
foreach(array_combine($post_ids, $metas) as $post_ids => $metas) {
update_post_meta( $post_id, \'_metadescs\', $meta);
}
// Did not work but an example of what I tried
foreach ($post_ids as $key=> $meta) {
update_post_meta( $post_id, \'_metadescs\', $meta);
}
die();
}
SO网友:majick
$post_id
在任何更新循环中都没有定义,这表明一个简单的循环不足以实现这一点。
由于要循环两个并发创建的数组,因此需要执行类似操作,使用索引匹配值:
for ( $i = 0; $i < count($post_ids); $i++ ) {
update_post_meta( $post_ids[$i], \'_metadescs\', $metas[$i] );
}
或者,您可以在循环中执行循环,并通过以下方式匹配索引:
foreach ( $post_ids as $i => $post_id ) {
foreach ( $metas as $j => $meta ) {
if ($i == $j) {update_post_meta( $post_id, \'_metadescs\', $meta );}
}
}