Support » Plugin: WP Telegram (Auto Post and Notifications) » rest api custom endpoint

  • Resolved misaghlb

    (@misaghlb)


    hi
    i have a custom endpoint for creating posts, the post will create successfully and published but won’t send to telegram.
    if i use the default endpoint -> POST /wp/v2/posts works fine and sends the post to Telegram.
    my Endpoint code is :

    
    
    function mycustom( WP_REST_Request $request ){
    	$my_post = array(
    		'post_title'    => $request['title'],
    		'post_content'  => $request['content'],
    		'post_status'   => $request['status'],
    		'post_excerpt'   => $request['excerpt'],
    		'post_author'   => 1,
    		'tax_input' => array( 'category' => $request['category'], 'post_tag' => $request['tags'])
    	);
    
    	// Insert the post into the database
    	$pid = wp_insert_post( $my_post );
    
    	return "ok";
    }
    
      add_action( 'rest_api_init', function () {
    	register_rest_route( 'wp/v2', '/mycustom', array(
    	  'methods' => 'POST',
    	  'callback' => 'cpost',
    	) );
      } );
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Manzoor Wani

    (@manzoorwanijk)

    Hello,
    Yes, the plugin expects every REST request to be from the default posts endpoint, so that it hooks into rest_after_insert_{$post_type} action.

    I would suggest to do the same in your custom endpoint callback. Add this line after wp_insert_post:

    do_action( "rest_after_insert_{$my_post['post_type']}", get_post( $pid ), $request, true );

    Make sure to add post_type to post array.

    misaghlb

    (@misaghlb)

    it’s solved, thanks a lot for your awesome support and plugin.

    Plugin Author Manzoor Wani

    (@manzoorwanijk)

    I’m glad it’s resolved 🙂

    You are welcome. You may write a review for the plugin 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.