• Resolved hannahestes

    (@hannahestes)


    Hello,

    I am very new to wordpress development and I am trying to use the plugin to create a REST endpoint that will allow my users to rename media. Here is what my route registration looks like:

    add_action( 'rest_api_init', 'custom_routes');
    function custom_routes(){
    	register_rest_route('custom/media/v1', 'rename_media',array(
    		'methods'  => array( 'POST' ),
    		'callback' => 'media_rename',
    		'args' => array('media_id' => 'string',
    						'new_name' => 'string')
    	));
    }
    
    function media_rename($request) {
        $params = wp_parse_args( $request->get_params(), [
        	'media_id' => '-1',
        	'new_name' => ''
      	] );
    
        $args = array(
            'media_id' => $params['media_id'],
    		'new_name' => $params['new_name']
        );
    	
    	$result = -1;
    	if (function_exists('do_rename'))
    	{
    	    $result = do_rename($args['media_id'], $args['new_name']);
    	}
        
        if ($result != 1) {
        	return new WP_Error( 'empty_category', 'no image found', 		array('status' => 404) );
        }
    
        $response = new WP_REST_Response($params['new_name']);
        $response->set_status(200);
    
        return $response;
    }

    `

    However, when I call function_exists('do_rename') from my media_rename function it returns false. If I don’t do the check and try to call do_rename then I get an internal server error. Any insight as to what I am doing wrong or how I can fix this?

    • This topic was modified 2 years, 6 months ago by hannahestes.
Viewing 1 replies (of 1 total)
  • Plugin Author crossi72

    (@crossi72)

    Hi @hannahestes,
    the function do_rename is a static method wrapped inside the Phoenix_Media_Rename class, so you have to call it using Phoenix_Media_Rename::do_rename

    I will this information to the readme, it is not clear how to call the function.

    Let me know if you need more information ??
    C.

Viewing 1 replies (of 1 total)
  • The topic ‘do_rename function does not exist’ is closed to new replies.