• Resolved ysnz

    (@ysnz)


    Hello Forum,

    I’m trying to create new redirections directly in a plugin via php and the WP_REST_Request-Class.

    I already could create a redirection, but I can’t add the target url properly…
    For me it only works to give an empty string as parameter. All other tries throw an error. Here the working code:

    $request = new WP_REST_Request( 'POST', '/redirection/v1/redirect' );
    $request->set_param('status', 'enabled');
    $request->set_param('position', 0);
    $matchdata = ['source' => [
        'flag_regex' => false,
        'flag_query' => 'ignore',
        'flag_case' => true,
        'flag_trailing' => false
    ]];
    $request->set_param('match_data', json_encode($matchdata));
    $request->set_param('regex', false);
    $request->set_param('url', '/here-we-come');
    $request->set_param('match_type', 'url');
    $request->set_param('title', 'test2');
    $request->set_param('group_id', 1);
    $request->set_param('action_type', 'url');
    $request->set_param('action_code', 301);
    $request->set_param('action_data', '');
    
    $response = rest_do_request( $request );

    I tried different ways to give the action_data, but none of them worked:

    $request->set_param('action_data', '["url": "/here-we-go"]');
    $request->set_param('action_data', {"url":"here-we-go"});
    $request->set_param('action_data', json_encode(['url' => '/here-we-go']));
    $request->set_param('action_data', json_encode("/here-we-go"));

    Anyone has this woking or know how to do it?

    • This topic was modified 5 years, 9 months ago by ysnz.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author John Godley

    (@johnny5)

    You can find developer information here:

    https://redirection.me/developer/

    You don’t need to JSON encode data being added, and the other data formats you’ve used make no sense – this is PHP, so you have to send PHP data.

    You can use your browser developer tools and see what data is being sent by Redirection.

    Thread Starter ysnz

    (@ysnz)

    Thanks for your answer!

    I thought I tried this too, but I was too confused 😀

    So here the working code for everybody else who wants a working code to start with.

    $request = new WP_REST_Request( 'POST', '/redirection/v1/redirect' );
    $request->set_param('status', 'enabled');
    $request->set_param('position', 0);
    $matchdata = ['source' => [
        'flag_regex' => false,
        'flag_query' => 'ignore',
        'flag_case' => true,
        'flag_trailing' => false
    ]];
    $request->set_param('match_data', $matchdata);
    $request->set_param('regex', false);
    $request->set_param('url', '/here-we-come');
    $request->set_param('match_type', 'url');
    $request->set_param('title', 'test2');
    $request->set_param('group_id', 1);
    $request->set_param('action_type', 'url');
    $request->set_param('action_code', 301);
    $request->set_param('action_data', ['url' => '/here-we-go']);
    
    $response = rest_do_request( $request );
    $server = rest_get_server();
    $data = $server->response_to_data( $response, false );
    // do whatever you want with the returned data
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘How to format action_data for WP_REST_Request’ is closed to new replies.