0

First and foremost - thank you to anyone and everyone who is taking the time to respond. I am a newer developer and I am sure I have a misunderstanding or something simple that I am missing. I have spent hours trying different things and I am just not sure what to try now. Below you can find my code and an explanation of my problem.

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.

With that in mind - I have created a very simple file with all the principals I am trying to achieve. The idea being that once I get this portion figured out, I will then figure out the proper loops to try and achieve my end goal. So below is the code as close to exact as I can get. Again, any help would be greatly appreciated - I have been testing for a while now and just cannot get this dang thing to work.

I have gotten the AJAX request to log that it was successful so I am totally lost because it doesn't save or update the database upon refresh.

    <?php
function ews_metas_add_admin_page() {
    // Adds plugin page to menu
    add_menu_page ('Effective Web Solutions Metas Descriptions', 'EWS Metas', 'manage_options', 'ews_meta_options_stephen_version', 'ews_meta_page_creation_stephen', get_template_directory_uri() . '/images/admin-ico-gray.png', 110 );
    //Activate custom settings
    add_action( 'admin_init', 'display_metas_fields');
}
add_action( 'admin_menu', 'ews_metas_add_admin_page'); ?>

<script>
    jQuery(document).ready(function() {
            jQuery(".toplevel_page_ews_meta_options_stephen_version form").submit(function() {
            var post_meta = jQuery("input.testing").val();
            var post_ID = jQuery("input.testing").attr("name");
            jQuery.ajax({
                type: "POST",
                url: 'https://dev.ewsproduction.com/dev6/ewstheme/wp-admin/admin-ajax.php',
                data: {
                    action: "update_meta_descriptions",
                    post_id: post_ID,
                    post_meta: post_meta,
                },
                success: function( data ) {
                //do something
                console.log('It worked!');
            }
        });
        return false;
        });
    });
</script>

<?php 
function ews_meta_page_creation_stephen() { ?>
<div>
    <?php settings_errors(); ?>
    <form method="post">
        <input id="meta" class="testing" type="text" name="4189" value="<?php echo esc_html( get_post_meta( 4189, '_metadescs', true ) ); ?>" />
        <?php submit_button();?>
    </form>
</div>
<?php } ?>

<?php add_action( 'wp_ajax_update_meta_descriptions', 'update_meta_descriptions' );
    function update_meta_descriptions() {
        $post_id = $_POST['post_id'];
        $meta = $_POST['post_meta'];
        update_post_meta( $post_ID, '_metadescs', $post_meta );
        echo 'Meta Updated';
    die(); 

} ?>

With all of that in mind - any help would be greatly appreciated. I have tried just about everything I can find and no luck.

Any insight on my overall goal is also apprecaite - what I am trying to do is eventually create a plugin that lists all the Meta and Page Title information from each page on a plugin page. Then I would like to be able to edit and update those values from said plugin page.

  • Thank you @Anujayweb! Your help totally solved it and things are now working. Thanks for your time and explanation - it was super helpful. – Stephen Burks Jul 23 at 15:06
1

You did some mistakes that I have described below:

  • First thing you have placed jQuery Ajax code on the wrong place.
  • On ajax callback function variable name mashup like $post_id defined and used on update time $post_ID; PHP is case sensitive so it considers 2 different variables.
  • In some places, not closed } brackets.

Here is the right code which I have modified in your code. Check this and let me know it's work for you or not. I have tested it works for me.

<?php /*Register the Menu*/ 
add_action( 'admin_menu', 'ews_metas_add_admin_page');
function ews_metas_add_admin_page() { 
// Adds plugin page to menu
add_menu_page ('Effective Web Solutions Metas Descriptions', 'EWS Metas', 'manage_options', 'ews_meta_options_stephen_version', 'ews_meta_page_creation_stephen', get_template_directory_uri() . '/images/admin-ico-gray.png', 110 );
} 

/*Page Html Render*/
function ews_meta_page_creation_stephen() { ?>
<div>
<?php settings_errors(); ?>
<form method="post">
    <input id="meta" class="testing" type="text" name="2316" value="<?php echo esc_html( get_post_meta( 4189, '_metadescs', true ) ); ?>" />
    <?php submit_button();?>
</form>
</div>
<script>
jQuery(document).ready( function($){ 
jQuery(".toplevel_page_ews_meta_options_stephen_version form").submit(function() {
        var post_meta = jQuery("input.testing").val();
        var post_ID = jQuery("input.testing").attr("name");
        jQuery.ajax({
            type: "POST",
            url: 'https://dev.ewsproduction.com/dev6/ewstheme/wp-admin/admin-ajax.php',
            data: {
                action: "update_meta_descriptions",
                post_id: post_ID,
                post_meta: post_meta,
            },
            success: function( data ) {
            //do something
            console.log('It worked!');
        }
    });
    return false;
    });
});
</script>
<?php }

/*Update the meta using Ajax request */
add_action( 'wp_ajax_update_meta_descriptions', 'update_meta_descriptions' );
function update_meta_descriptions() {
  global $wpdb;
  $post_id = $_POST['post_id'];
  $meta = $_POST['post_meta'];
  $details = update_post_meta( $post_id, '_metadescs', $meta );
  echo 'Meta Updated';
  die();
}?>
  • Please describe what has been done and why. – Max Yudin Jul 23 at 8:01
  • @MaxYudin I have updated my answer as per your request. Thank you for your comment. :) – Anujayweb Jul 23 at 9:43
  • @Stephen Burks Thank for your comment! Can you vote to the answer so another user-helpful it? – Anujayweb Jul 23 at 19:09
  • Your suggestions sounds reasonable, but I didn't test you code, so it's not my call to vote. Are you sure it works? Let's wait for OP reaction. – Max Yudin Jul 24 at 6:28
  • @MaxYudin - this did work well! Sorry for the late upvote - my profile is still new so I didn't realize I could up vote it! I do have another follow up question that I am asking if either of you have any interest in answering it would be greatly appreciated! I am just trying to figure out how to output and update the post meta if I have an array of data rather than just one value. Essentially, I can't get the DB to update when I am passing several values. – Stephen Burks Jul 24 at 22:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.