I'm trying to redirect user to search page having customized query. I'm stuck with wp_redirect() and getting error:
Fatal error: Uncaught Error: Call to undefined function wp_redirect()
Here is my redirection code:
customFile.php
<?php /* Template Name: customFile*/ ?>
<?php
// Logic to build $cSearch
// $cSearch is generated above in this file
$baseUrl = site_url().'/?s='. $cSearch;
wp_redirect( $baseUrl, 302 );
exit();
This file is required once in main plugin file.
I guessing that you try to use wp_redirect before the function included by wordpress.
Try this:
add_action( 'template_redirect', function() {
$baseUrl = site_url().'/?s='. $cSearch;
wp_redirect( $baseUrl, 302 );
exit();
} );
Try with this code
<?php /* Template Name: customFile*/ ?>
<?PHP
if (!defined('ABSPATH')) {
require_once(dirname(__FILE__) . '/wp-load.php');
}
// Logic to build $cSearch
// $cSearch is generated above in this file
$baseUrl = site_url().'/?s='. $cSearch;
wp_redirect( $baseUrl, 302 );
exit();
Related
I have a 404 page in my theme but I am not using that page. I have created a new 404 page in WordPress using wpbakery page builder. I need to know how can I redirect users on the new 404 page without a plugin?
You can use plugin 404page.
Or some code adapted from this plugin:
add_filter(
'404_template',
static function () {
global $wp_query;
$wp_query = new WP_Query();
$wp_query->query('page_id='.$pageID);
$wp_query->the_post();
$template = get_page_template();
rewind_posts();
add_filter(
'body_class',
static function ($classes) {
if (!in_array('error404', $classes, true)) {
$classes[] = 'error404';
}
return $classes;
}
);
return $template;
},
999
);
Crate 404page in the admin.
create a custom page template for that page.
add your custom 404 content
open 404.php file in your theme.
add this below code at the top of that file.
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".home_url('/404page/'));
exit();
try to find something that not found and you will be redirected to your custom 404 page
also, you can try this action hooks for redirect to custom 404 page. put this code in your function.php file. this is the replace option of above point number 5)
add_action( 'template_redirect', 'redreict_to_custom_404_page' );
function redreict_to_custom_404_page(){
// check if is a 404 error
if( is_404() ){
wp_redirect( home_url( '/404page/' ) );
exit();
}
}
Or if you want to create 404 page with WP bakery
Create a private 404page and build with WP bakery in the admin.
open 404.php file and get content of 404page by code below
$page_id = 123; // 404page id
$page = get_post( $page_id );
$content = $page->post_content;
echo $content;
I want to edit this link via wordpress
http://domain-name.com/self-coaching-tips/media/
when I click on edit, I am able to edit the MEDIA part only. I want to update the whole link to
http://domain-name.com/media/
How can I do this?
Add to your functions.php file:
function cstm_url_redirects() {
$redirect_rules = array(
array('old'=>'/self-coaching-tips/media/','new'=>'/media/'),
//array('old'=>'/some-other-old/page/','new'=>'/some/new/page/'),
);
foreach( $redirect_rules as $rule ) :
// if URL of request matches with the one from the array, then redirect
if( urldecode($_SERVER['REQUEST_URI']) == $rule['old'] ) :
wp_redirect( site_url( $rule['new']) , 301 );
exit();
endif;
endforeach;
}
add_action('template_redirect', 'cstm_url_redirects');
More examples here
I want to redirect admin users to a maintenance page (https://mysite/maintenance/), but firefox tells me the redirection is not correctly made
add_action( 'template_redirect', 'custom_redirect' );
function custom_redirect()
{
if (current_user_can('administrator')) {
wp_redirect( home_url('/maintenance/') );
exit;
}
}
Have you an idea ?
You should be using the template_include filter for this:
add_filter('template_include', 'wpse_44239_template_include', 1, 1);
function wpse_44239_template_include($template){
if (current_user_can('administrator')) {
wp_redirect( home_url('/maintenance/') );
exit;
}
return $template;
}
template_redirect is the action called directly before headers are sent for the output of the rendered template. It's a convenient hook to do 404 redirects, etc... but shouldn't be used for including other templates paths as WordPress does this innately with the 'template_include' filter.
template_include and single_template hooks deal ONLY with the path of the template used for rendering the content. This is the proper place to adjust a template path.
What is the specific error Firefox is reporting? Sounds like it could be an infinite redirect loop. I would suggest adding a check to make sure you're not already on the maintenance page, ie:
add_action( 'template_redirect', 'custom_redirect' );
function custom_redirect()
{
if (current_user_can('administrator'))
{
global $wp;
$current_url = home_url( $wp->request );
$position = strpos( $current_url , '/maintenance/' );
if ($position===FALSE) {
wp_redirect( home_url('/maintenance/') );
exit;
}
}
}
I'm writing an Wordpress plugin. With this plugin I update some data. The query and updating works fine, but my header("location: url"); doesn't work. If I place an echo, it won't give any error that the headers already send. It looks it doesn't do anything with those lines. My code...
<?php require_once('../../../wp-config.php');
$baanstatus_table=$wpdb->prefix . 'baanstatus';
$id = $_GET['id'];
$bijgewerkt =$_GET['bijgewerkt'];
$baanstatus= $_GET['baanstatus'];
$handicarts = $_GET['handicarts'];
$trolleys = $_GET['trolleys'];
$winterontheffing = $_GET['winterontheffing'];
$zomergreens = $_GET['zomergreens'];
$qualifying = $_GET['qualifying'];
$onderhoud_greens = $_GET['onderhoud_greens'];
$onderhoud_anders = $_GET['onderhoud_anders'];
$opmerkingen = $_GET['opmerkingen'];
global $wpdb;
$data_array =array('id' => $id,
'bijgewerkt' => $bijgewerkt,
'baanstatus' => $baanstatus,
'handicarts' => $handicarts,
'trolleys' => $trolleys,
'winterontheffing' =>$winterontheffing,
'zomergreens' =>$zomergreens,
'qualifying' =>$qualifying,
'onderhoud_greens' =>$onderhoud_greens,
'onderhoud_anders' =>$onderhoud_anders,
'opmerkingen' =>$opmerkingen
);
$where =array('id' => $id);
$wpdb->update( $baanstatus_table, $data_array, $where );
header("location:http://almeerderhout.fcklap.com/wp-admin/options-general.php?page=my-unique-identifier");
exit();
?>
Perhaps you should try the javascript, instead of PHP location.
<?php
echo '<script>location.href="http://almeerderhout.fcklap.com/wp-admin/options-general.php?page=my-unique-identifier";</script>';
?>
The below code will help you
<?php
wp_redirect( $location, $status );
exit;
?>
The above wordpress function will use to redirect Codex Link function reference
You should hook your plugin to a proper Wordpress action to avoid the "headers already sent error" when trying to redirect.
I found that a good place to perform redirects is the template_redirect action, so you can write something like this:
function do_something_then_redirect() {
// do something with $_GET or $_POST data
// then redirect to some url defined in the $redirect_url variable
wp_redirect($redirect_url);
die;
}
add_action('template_redirect', 'do_something_then_redirect');
My problem is that I use dynamic css file in the WordPress theme that is loaded with Ajax. However, it loads this same dynamic css file for backend also. How do I modify my code that it loads dynamic css file only for frontend, not for the backend. Here's my code:
wp_enqueue_style('dynamic-css',
admin_url('admin-ajax.php?action=dynamic_css'));
function dynaminc_css() {
require(get_template_directory().'/dynamic-css.php');
exit;
}
add_action( 'wp_ajax_dynamic_css', 'dynaminc_css' );
add_action( 'wp_ajax_nopriv_dynamic_css', 'dynaminc_css' );
}
Here's a working example with inline comments:
<?php
/*
Plugin Name: Dynamic CSS using Ajax
Plugin URI: https://github.com/soderlind/
Description:
Author: Per Soderlind
Version: 0.1.0
Author URI: http://soderlind.no
*/
if ( !defined( 'ABSPATH' ) ) {
die( 'Cheating, are we?' );
}
define( 'DYNAMICCSS_VERSION', '0.1.0' );
function dynamic_css_enqueue() {
wp_enqueue_style( 'dynamic-flags', admin_url( 'admin-ajax.php' ).'?action=dynamic_css&_wpnonce=' . wp_create_nonce( 'dynamic-css-nonce' ), false, DYNAMICCSS_VERSION );
}
function dynamic_css() { // Don't wrap function dynamic_css() in if(!is_admin()){ , the call from admin-ajax.php will be from admin
$nonce = $_REQUEST['_wpnonce'];
if ( ! wp_verify_nonce( $nonce, 'dynamic-css-nonce' ) ) {
die( 'invalid nonce' );
} else {
/**
* NOTE: Using require or include to call an URL ,created by plugins_url() or get_template_directory(), can create the following error:
* Warning: require(): http:// wrapper is disabled in the server configuration by allow_url_include=0
* Warning: require(http://domain/path/flags/css.php): failed to open stream: no suitable wrapper could be found
* Fatal error: require(): Failed opening required 'http://domain/path/css.php'
*/
require dirname( __FILE__ ) . '/css.php'; //use echo, printf etc in css.php and write to standard out.
}
exit;
}
add_action( 'wp_ajax_dynamic_css', 'dynamic_css' );
add_action( 'wp_ajax_nopriv_dynamic_css', 'dynamic_css' );
add_action( 'wp_enqueue_scripts', 'dynamic_css_enqueue' ); //wp_enqueue_scripts = load on front-end
The is_admin() function is what you are looking for
if(!is_admin()){
wp_enqueue_style('dynamic-css',
admin_url('admin-ajax.php?action=dynamic_css'));
function dynaminc_css() {
require(get_template_directory().'/dynamic-css.php');
exit;
}
add_action( 'wp_ajax_dynamic_css', 'dynaminc_css' );
add_action( 'wp_ajax_nopriv_dynamic_css', 'dynaminc_css' );
}
Anything inside there will only execute if not in the administration panel.
http://codex.wordpress.org/Function_Reference/is_admin