• Resolved malcolmdixon

    (@malcolmdixon)


    Hi folks,

    I need some advice on using Ajax with WordPress as I’ve read, watched and experimented with different techniques and I’m not clear on the best approach.

    I am new to WordPress development and web development in general, so I don’t want to fall into any pits later on, so any guidance would be appreciated.

    After watching a YouTube video https://www.youtube.com/watch?v=OwBBxwmG49w, it seems that the best appoach is to use the Rewrite API.

    So, I wrote code to create the necessary endpoints etc for the required small API, all GET requests.

      add_action('init', function () {
        add_rewrite_tag('%cpt_id%', '([0-9]+)');
        add_rewrite_tag('%cpt%', '([a-z]+)');
    
        add_rewrite_rule(self::API_BASE_URL . '([a-z]+)/([0-9]+)/?', 'index.php?cpt=$matches[1]&cpt_id=$matches[2]', 'top');
        add_rewrite_rule(self::API_BASE_URL . '([a-z]+)/?$', 'index.php?cpt=$matches[1]', 'top');
      });
    
      add_filter( 'query_vars', function ($vars) {
        array_push($vars, 'make', 'plug_type');
        return $vars;
      });

    This seems to work so far with the limited testing, allowing me to route depending on the custom post type but I do feel that I’m implementing a REST API that’s already available albeit that supplies much more data than necessary. Although you can use the query variable _fields to limit data. But then to get the featured image would be another call as far as I can tell.

    Someone said to use WP_Query object too but I’m far more comfortable using SQL directly on $wpdb, which is easy enough to do for the above:

    $results = $wpdb->get_results("SELECT posts.post_title, posts.ID, meta.meta_value AS img_src FROM $wpdb->posts AS posts LEFT JOIN $wpdb->postmeta ON post_id = posts.ID AND meta_key = '_thumbnail_id' LEFT JOIN $wpdb->posts AS attachment ON attachment.ID = meta_value LEFT JOIN $wpdb->postmeta as meta ON meta.post_id = attachment.ID AND meta.meta_key = '_wp_attached_file' WHERE posts.post_type = 'makes';", OBJECT_K);

    Probably should have done a subquery but the above works.

    So, my question is do I use:

    1) The WP REST API.

    2) The Rewrite API and $wpdb, which I have sort of working now.

    3) A PHP file using require_once(“wp-load.php”); and $wpdb called directly. This seems to work but I’m unsure about security.

    <?php
      require_once("wp-load.php");
    
      global $wpdb;
    
      $results = $wpdb->get_results("SELECT posts.post_title, posts.ID, meta.meta_value AS img_src FROM $wpdb->posts AS posts LEFT JOIN $wpdb->postmeta ON post_id = posts.ID AND meta_key = '_thumbnail_id' LEFT JOIN $wpdb->posts AS attachment ON attachment.ID = meta_value LEFT JOIN $wpdb->postmeta as meta ON meta.post_id = attachment.ID AND meta.meta_key = '_wp_attached_file' WHERE posts.post_type = 'makes';", OBJECT_K);
    
      $message = json_encode($results);
    
      wp_send_json_success([
        'message' => $message
      ]);
    ?>

    I haven’t even attempted to try the admin-ajax.php option due to the YouTube video showing how slow it is compared to the other options and from what I’ve “skim” read seems complex anyway.

    I appreciate anyone spending their time to advise me on this.

Viewing 2 replies - 16 through 17 (of 17 total)
  • Moderator bcworkz

    (@bcworkz)

    They’re not drop-in replacements for index.php, they require a different approach. The end result is no different, so I don’t blame you for leaving it be. The only real benefit is it’s maybe a more proper “WordPressy” approach. But hooking “template_redirect” isn’t that far off either. If I were in your situation I’d probably leave it be as well.

    Thread Starter malcolmdixon

    (@malcolmdixon)

    Oh I see, thought there might be more to it. When I get time I’ll definitely look into it, as I’m beginning to enjoy this web development malarkey a lot different to Cobol and RPG II lol.

Viewing 2 replies - 16 through 17 (of 17 total)
  • The topic ‘Ajax best approach’ is closed to new replies.