• Resolved marcusquinn

    (@surferking)


    It would be super handy to be able to use the links button in Gutenberg to type the first letters of the link being sought, and Pretty Links be available in the auto-complete options.

    I believe this means making the Pretty Links Post Type Public.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Tyler

    (@tylerthedude)

    Hi @surferking,

    It’s nice to chat with you again. Could you please try adding the code I provided in the other thread you have open and see if that allows the pretty links to show up within that search?

    I hope this helps!

    Kind regards,

    Thread Starter marcusquinn

    (@surferking)

    Hey @tylerthedude Yes, the code in the other thread worked for that RankMath Instant Indexing need. However, I think we need a little more. Showing to the REST API makes it available to Gutenberg:

    add_filter('register_post_type_args', function($args, $post_type) {
        if ($post_type == 'pretty-link') {
            $args['public'] = true;
            $args['show_in_rest'] = true;
        return $args;
    }, 10, 2);
    

    However, that auto-completes the URL as: /?pretty-link=linkedin-profile for example.

    Whereas, what we need is the actual Pretty Link URL, eg: /link/linkedin-profile

    ChatGTP suggest something like the following, although it’s late here, so I only tested once, but it didn’t work, and I’ve not had a chance to check the actual meta key value. Hope it makes sense…

    function register_pretty_link_rest_field() {
        register_rest_field('pretty-link', 'pretty_link', [
            'get_callback' => function($object) {
                // Assuming 'pretty_link_meta_key' is the meta key for your "Pretty Link" field
                return get_post_meta($object['id'], 'pretty_link_meta_key', true);
            },
            'schema' => null,
        ]);
    }
    add_action('rest_api_init', 'register_pretty_link_rest_field');
    

    Again, I feel a good value-adding feature, if you can ??

    Tyler

    (@tylerthedude)

    Hi @surferking,

    Thank you for getting back to me. Could you please try using this bit of code in place of the one provided to you by ChatGPT? Once this code has been added, then you should be able to search for those pretty links and its actual URL will be used instead of the default one (site.com/?pretty-link=slug):

    add_filter('post_type_link', function($post_link, $post) {
      global $prli_link, $prli_blogurl;
    
      if($post->post_type == 'pretty-link') {
        $pretty_link = $prli_link->get_one_by('link_cpt_id', $post->ID);
    
        if(!$pretty_link) {
          return $post_link;
        }
    
        $post_link = $prli_blogurl . PrliUtils::get_permalink_pre_slug_uri() . $pretty_link->slug;
      }
    
      return $post_link;
    }, 10, 2);

    I hope this helps!

    Kind regards,

    Thread Starter marcusquinn

    (@surferking)

    Brilliant – that works perfectly! ?? Very “pretty” !! ??

    Let me know if it makes it into a core release as an option, so I can later remove.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Enable Pretty Links to be searched through Gutenberg Link buttons’ is closed to new replies.