• Resolved sipw

    (@sipw)


    I’m attempting to programmatically create redirects. While hooks such as redirection_request_url and redirection_url_source are firing correctly, the primary function redirection_create_redirect is not available during runtime, even after the plugin has fully initialised. Am I missing something?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author John Godley

    (@johnny5)

    Without knowing what you are doing I can’t say if you are missing anything. redirection_create_redirect is fired every time a redirect is created using the Redirection code.

    If you are programmatically trying to create redirects why are you using hooks? There’s an API that can create redirects directly.

    Thread Starter sipw

    (@sipw)

    Thanks for the swift response!

    Initially, we explored using hooks because we assumed they were the intended way. They offered potential advantages such as better performance, no need for authentication, and no additional HTTP requests. Plus, we referred to your hooks documentation, which seemed to suggest they might be appropriate.

    We’re creating redirects for URLs that are removed during an API call. When these URLs are no longer available in the API, we need to create redirects to a fallback page.

    However, we’ve since shifted to using the REST API. The issue we’re facing with the API is persistent timeouts (cURL error 28), even with extended timeouts and valid authentication. The /wp-json/redirection/v1/redirect endpoint doesn’t respond within the allocated time, despite other REST API functionality on the site working fine. We’re trying to determine if this is a plugin-specific issue, server configuration problem, or something else.

    Plugin Author John Godley

    (@johnny5)

    The hooks are just WordPress action and filter hooks. They are fired when you do something in Redirection and allow you to customise the default behaviour.

    The API is used by the plugin itself, so if it is working fine then the API is working fine and the problem is elsewhere.

    Also, you can call API endpoints internally within WordPress using one of the built-in API functions. If your code is running in WP there is no need to make an external request.

    Thread Starter sipw

    (@sipw)

    Ok many thanks. The only issue we’re hitting now is with passing the action_data – it’s being saved as null. Redirections are created, just obviously with no target url.

    {
    “url”: “/test-source”,
    “action_data”: “/test-target”,
    “match_type”: “url”,
    “action_type”: “url”,
    “group_id”: 1,
    “enabled”: true
    }

    {
    "id": 6467,
    "url": "/test-source",
    "match_url": "/test-source",
    "match_data": {
    "source": {
    "flag_query": "exact",
    "flag_case": true,
    "flag_trailing": true,
    "flag_regex": false
    }
    },
    "action_code": 301,
    "action_type": "url",
    "action_data": null,
    "match_type": "url",
    "title": "",
    "hits": 0,
    "regex": false,
    "group_id": 1,
    "position": 0,
    "last_access": "-",
    "enabled": true
    }
    Thread Starter sipw

    (@sipw)

    Got it. The?"action_data"?field must be an object containing a?"url"?key, not just a string.

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