Skip to content
PHP Logo

How can custom fields in WordPress be utilized for conditional redirects in PHP?

To utilize custom fields in WordPress for conditional redirects in PHP, you can create a custom field in your post or page where you specify the URL you want to redirect to. Then, in your theme's template file, you can check if the custom field exists and if it does, perform a redirect to the specified URL.

php
// Get the custom field value
$redirect_url = get_post_meta(get_the_ID(), 'custom_redirect_url', true);

// Check if the custom field value exists
if ($redirect_url) {
    // Perform the redirect
    wp_redirect($redirect_url);
    exit;
}