• Resolved Howdy_McGee

    (@howdy_mcgee)


    Hello,

    I have some Custom Endpoints and I would like to add those endpoints into the TinyMCE Inline Link list. For example, when you have something like Classic Editor installed and add a new link, it searches all public pages and post types to give you a list of options. I’m looking to inject my own Custom Endpoints into that list of options to choose from. Any ideas or directions would be super helpful!

Viewing 3 replies - 1 through 3 (of 3 total)
  • I think it doesn’t use the REST API, but it has an AJAX handler that might have a filter.

    Moderator bcworkz

    (@bcworkz)

    Ajax handler is wp_ajax_wp_link_ajax(), but no immediate filter there. You could use ‘wp_link_query’ filter, which passes an array of post data. For API endpoints you’d need to compose fake post data. I think post $ID = 0 should be OK for all, but untested.

    Thread Starter Howdy_McGee

    (@howdy_mcgee)

    Thanks! I was able to get it to work as using the wp_link_query filter. I’ve provided an example below that may not make much sense out of context.

    /**
     * Add Endpoints to Inline Linking Tool
     * 
     * @param Array $results
     * @param Array $query
     * 
     * @return Array $results
     */
    function prefix_add_endpoints_to_inline_links( $results, $query ) {
    	
    	$endpoints		= prefix_get_endpoints();	// Array of endpoint data keyed by group then by endpoint
    	$possibilities	= array();
    	
    	foreach( $endpoints as $group => $groupdata ) {
    		foreach( $groupdata as $endpoint => $epdata ) {
    			
    			if( ! $epdata['show_in_nav'] ) { // Custom flag to skip showing endpoint
    				continue;
    				
    			// Search Endpoint Nicename against query
    			} else if( false !== stripos( $epdata['nicename'], $query['s'] ) ) {
    			
    				$possibilities[] = array(
    					'ID'		=> 0,
    					'title'		=> $epdata['nicename'],
    					'permalink' => prefix_endpoint_url( $endpoint, $group ),	// Grab the endpoint URL
    					'info'		=> 'EP ' . ucfirst( $group ),
    				);
    				
    			}
    			
    		}
    	}
    	
    	$results = array_merge( $results, $possibilities );
    	array_multisort( array_column( $results, 'title' ), SORT_ASC, $results );
    	
    	return $results;
    	
    }
    add_filter( 'wp_link_query', 'prefix_add_endpoints_to_inline_links', 10, 2 );

    Thanks again!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Inject into TinyMCE Inline Link Query’ is closed to new replies.