• Resolved tedpriv

    (@tedpriv)


    With Ajax enabled, the [yasr_visitor_votes] shortcode has an empty data-cpt attribute in the generated HTML, causing the URL for the WordPress API to be incorrect and return a 404 error. The API URL specifies ‘posts’ instead of the name of the custom post type.

    I’ve created a fresh WordPress 5.6.1 install with no plugins other than CPT UI and YASR 2.6.1.

    The request that fails has ‘posts’ in the URL and is: https://<site>/wp-json/wp/v2/posts/112?_fields=yasr_visitor_votes&_wpnonce=62077aa643

    The (manually created) request that succeeds has the custom post type name in the URL and is: https://<site>/wp-json/wp/v2/<cpt name>/112?_fields=yasr_visitor_votes&_wpnonce=62077aa643

    The URL portion in question is generated in yasr_front.js:
    let cpt = yasrRaterVVInDom.item(i).getAttribute(‘data-cpt’);

    The generated HTML has an empty ‘data-cpt’ attribute:

    <!–Yasr Visitor Votes Shortcode–><div id=’yasr_visitor_votes_747′ class=’yasr-visitor-votes’><div class=”yasr-custom-text-vv-before yasr-custom-text-vv-before-747″>Click to rate this post!</div><div id=’yasr-visitor-votes-rater-f36c540a449a0′
    class=’yasr-rater-stars-vv’
    data-rater-postid=’747′
    data-rating=’4.4′
    data-rater-starsize=’32’
    data-rater-readonly=’false’
    data-rater-nonce=’fd37a17db7′
    data-issingular=’true’
    data-cpt=”>
    </div>

    This HTML is generated by YasrVisitorVotes.php:

    $this->shortcode_html .= “<div id=’$htmlid’
    class=’yasr-rater-stars-vv’
    data-rater-postid=’$this->post_id’
    data-rating=’$average_rating’
    data-rater-starsize='”.$this->starSize().”‘
    data-rater-readonly=’$this->readonly’
    data-rater-nonce=’$this->ajax_nonce_visitor’
    data-issingular=’$this->is_singular’
    data-cpt=’$this->post_type’>
    </div>”;

    The ‘post_type’ property is filled in with this code (YasrShortcode.php, line 69):

    $this->post_type = YasrCustomPostTypes::returnBaseUrl($this->post_id);

    And the returnBaseUrl function is as follows (YasrCustomPostTypes.php, line 105):

    public static function returnBaseUrl($post_id=false) {
    if($post_id === false || !is_int($post_id)) {
    $post_id = get_the_ID();
    }
    $post_type_object = get_post_type_object(get_post_type($post_id));
    //if get_post_type_object fails for some reason, return ‘posts’
    if($post_type_object === null) {
    return ‘posts’;
    }
    return $post_type_object->rest_base;
    }

    At this point it starts calling the WordPress API.

    Thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Ajax URL is incorrect for custom post type’ is closed to new replies.