admin管理员组文章数量:1418592
Thanks in advance to anyone and everyone who is taking the time to respond. So here is what I am trying to achieve: Essentially I am trying to create a plugin to just edit all the meta descriptions on my pages from a plugin admin page.
I am doing this by using the following logic - print all the metas from the database on a page. Using jQuery AJAX get all of the page ids and values from said page. On submit - push all the updated values to their respective spots in the database and update those meta descriptions.
I am not sure if I have flawed code on a conceptual level or if there is just something basic I am missing but below is the code that I have written. I have tried reading and testing just about everything I can and no avail. Any enlightenment would be greatly apprecaite.
Please note - my callback function has three foreach loops - none of which work but I left them so that you can see a few things that I have tried.
Thanks again!
<!-- 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=".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: '.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();
}
Thanks in advance to anyone and everyone who is taking the time to respond. So here is what I am trying to achieve: Essentially I am trying to create a plugin to just edit all the meta descriptions on my pages from a plugin admin page.
I am doing this by using the following logic - print all the metas from the database on a page. Using jQuery AJAX get all of the page ids and values from said page. On submit - push all the updated values to their respective spots in the database and update those meta descriptions.
I am not sure if I have flawed code on a conceptual level or if there is just something basic I am missing but below is the code that I have written. I have tried reading and testing just about everything I can and no avail. Any enlightenment would be greatly apprecaite.
Please note - my callback function has three foreach loops - none of which work but I left them so that you can see a few things that I have tried.
Thanks again!
<!-- 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/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/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();
}
Share
Improve this question
asked Jul 24, 2019 at 19:12
Stephen BurksStephen Burks
33 bronze badges
1 Answer
Reset to default 0$post_id
is never defined in any of your update loops, and this shows that a simple single loop is not enough to achive this.
Since you are looping two concurrently created arrays, you would need to do something like this instead, using the index to match values:
for ( $i = 0; $i < count($post_ids); $i++ ) {
update_post_meta( $post_ids[$i], '_metadescs', $metas[$i] );
}
Alternatively you could do a loop within a loop and match the index this way:
foreach ( $post_ids as $i => $post_id ) {
foreach ( $metas as $j => $meta ) {
if ($i == $j) {update_post_meta( $post_id, '_metadescs', $meta );}
}
}
本文标签: phpupdatepostmeta Not Processing Array Data (Not Sure What I Am Missing)
版权声明:本文标题:php - update_post_meta Not Processing Array Data (Not Sure What I Am Missing) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745297762a2652180.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论