Error with wp_redirect - wordpress

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;
}
}
}

Related

Wordpress redirect specific link to custom link

I have a wordpress website and I want to redirect a specific link to my custom link. I do not want to use any plugin and want to achieve this by writing code in functions.php file. The code that I tried is writen below
function custom_redirect() {
if(is_page(7)) {
wp_redirect('http://example.com/my-account/orders/', 301);
exit();
}
}
add_action ('template_redirect', 'custom_redirect');
Now the page, ( (is_page(7)) ), from which I want to redirect the users has the url which is http://www.example.com/my-account/ and I want to redirect them to http://www.example.com/my-account/orders. I tried the site_url('/my-account/') function also but unable to achieve that.
The problem is when you test for is_page('my-account') it will return true even when you are viewing an WooCommerce account endpoint. The workaround is to check the global $wp variable for the requested page slug.
function custom_redirect() {
global $wp;
if( $wp->request == 'my-account' ) {
wp_redirect( site_url( 'my-account/orders/' ) );
exit;
}
}
add_action ('template_redirect', 'custom_redirect');
i'm not sure that i understood, but i propose this:
function custom_redirect() {
if(is_page(7)) {
header('Location: http://example.com/my-account/orders/');
exit();
}
}
add_action ('template_redirect', 'custom_redirect');

How to Automatically Remove Default Video Links in WordPress

I am looking for the user to redirect to the home page when the user tries to access the video permalink.
function wpb_imagelink_setup() {
$image_set = get_option( 'image_default_link_type' );
if ($image_set !== 'none') {
update_option('image_default_link_type', 'none');
}
}
add_action('admin_init', 'wpb_imagelink_setup', 10);
I tried the above code, but it doesn't work
Actually you need to redirect the users via template_redirect hook that you can use as per below.
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
$image_set = get_option( 'image_default_link_type' );
if ( is_page($image_set) && ! is_user_logged_in() ) {
wp_redirect( 'http://www.yourhomepage.com/', 301 );
exit;
}
}
Check the $image_set is getting correct URL.

how to change search permalink in wordpress?

I have custom search page, that have permalink http://mywebsite.com/custom-search/
What should I do to pass the search keyword as a parameter, like this: http://mywebsite.com/custom-search/keyword
I get error 404 page. Or may be there a way to change standard permalink /search/ to /custom-search/ ?
You should use rewrite endpoints
A sample code :
/*!
* URL rewrite
*/
function my_custom_rewrite_rules() {
$page_id = 123;
$page_path = get_page_uri( $page_id );
add_rewrite_endpoint( 'keyword', EP_PAGES );
add_rewrite_rule('^'. $page_path .'/(.*)/?', 'index.php?page_id=' . $page_id . '&keyword=$matches[1]', 'top');
}
add_action('init', 'my_custom_rewrite_rules');
and then add it as a query_var
function my_custom_query_vars($vars) {
if( isset( $_GET['keyword'] ) && !empty( $_GET['keyword'] ) ) {
$vars[] = 'keyword';
}
return $vars;
}
add_filter( 'query_vars', 'my_custom_query_vars', 10, 1 );
you will be able to retrieve the value of the passed keyword via get_query_var("keyword")
hope it helps
Note : You must update your permalinks structure or use flush_rewrite_rules(); after adding these codes
just changed search base with function
function vital_custom_search_base() {
$GLOBALS['wp_rewrite']->search_base = 'custom-search';
}
add_action( 'init', 'vital_custom_search_base' );
function only works after resave in settings > permalinks

Add custom frontend page without menu item - wordpress

Im trying to do something like this.
Add "custom page" without page
I know about adding a wordpress page from admin panel, Pages->Add New, and then link this page to PHP file using the slug. I've already done that. I just want to make this page work without adding it from admin panel, in case if page gets deleted from admin panel it won't work even if exists in the directory.
Please let me know if my question isn't clear enough. Any help is highly appreciated.
Thanks!
Update:
Thanks to #Mike i was able to solve the problem by modifying his code. I just had to add add_rewrite_rule() and its working good now. Don't forget to flush permalinks.
function add_application_endpoint() {
add_rewrite_endpoint( 'view', EP_PERMALINK );
}
add_action( 'init', 'add_application_endpoint' );
function add_endpoint_queryvar( $query_vars ) {
$query_vars[] = 'view';
$query_vars[] = 'ptag';
$query_vars[] = 'product_cat';
return $query_vars;
}
add_filter( 'query_vars', 'add_endpoint_queryvar' );
add_rewrite_rule( '^view/([^/]+)/([^/]+)/?$', 'index.php?pagename=custom-product-tags&ptag=$matches[1]&product_cat=$matches[2]', 'top' );
/**
* Setting up job app template redirect for custom end point rewrite
*/
function job_application_template_redirect() {
global $wp_query;
if ( $wp_query->query_vars['name'] != 'custom-product-tags' ) {
return;
}
include dirname( __FILE__ ) . '/page-custom-product-tags.php';
exit;
}
add_action( 'template_redirect', 'job_application_template_redirect' );
You can do it by creating a custom endpoint and setting up a template redirect in your functions.php file.. Here is an example for a job application page. With this code added to my functions.php file, if I visit '/apply' on my site, the page-job_application.php template is rendered.
Hope this works for your needs.
/**
* Rewrite custom endpoint for job post applications
*/
function add_application_endpoint() {
add_rewrite_endpoint('apply', EP_PERMALINK);
}
add_action( 'init', 'add_application_endpoint');
/**
* Register our custom endpoint as a query var
*/
function add_endpoint_queryvar( $query_vars ) {
$query_vars[] = 'apply';
return $query_vars;
}
add_filter( 'query_vars', 'add_endpoint_queryvar' );
/**
* Setting up job app template redirect for custom end point rewrite
*/
function job_application_template_redirect() {
global $wp_query;
if ( ! isset( $wp_query->query_vars['apply'] ) || ! is_singular() )
return;
include dirname( __FILE__ ) . '/page-job_application.php';
exit;
}
add_action( 'template_redirect', 'job_application_template_redirect' );

How to add CSS only to the options page of a plugin in WordPress?

I want to add a stylesheet for the options_page of my plugin only. But how to do that? My code so far:
function add_options_page_style() {
wp_register_style('options_page_style', plugins_url('css/options_style.css',__FILE__));
wp_enqueue_style('options_page_style');
}
add_action( 'admin_menu', 'add_options_page_style' );
I could place an if statement before the line with add_action... but I'm not sure how to filter my options page. I already tried the $pagename variable and also this line: $wp_query->queried_object->post_name; but it didn't work.
The filter $_GET['page'] does work but might break in future versions.
Somewhere you'll be registering page like this:-
function register_page(){
global $page_hook_suffix;
$page_hook_suffix = add_options_page('Your_plugin', 'Your_plugin', 'manage_options', __FILE__, 'display_form');
}
add_action('admin_menu', 'register_page');
And while enqueueing script you'll do something like this:-
function my_enqueue($hook) {
global $page_hook_suffix;
if( $hook != $page_hook_suffix )
return;
wp_register_style('options_page_style', plugins_url('css/options_style.css',__FILE__));
wp_enqueue_style('options_page_style');
}
add_action( 'admin_enqueue_scripts', 'my_enqueue' );
function load_custom_wp_admin_style($hook) {
// Load only on ?page=mypluginname
if($hook != 'toplevel_page_mypluginname') {
return;
}
wp_enqueue_style( 'custom_wp_admin_css', plugins_url('admin-style.css', __FILE__) );
}
add_action( 'admin_enqueue_scripts', 'load_custom_wp_admin_style' );
add your page slug as suffix to toplevel_page_
e.g. if page slug is this-plugin-options
then
if($hook != 'toplevel_page_this-plugin-options') {
return;
}
here is wordpress doc with
Example: Load CSS File on All Admin Pages,
Example: Load CSS File from a plugin on specific Admin Page

Resources