How to redirect based on URL - wordpress

I am using Wp Store Locator plugin.And I have Modified it according my need.But I am stuck in redirection.
Basically this plugin works with short code.So at the time of listing my URL is like that : localhost/wordpress/?page_id=391
Now there is one link which is redirects me to http://localhost/wordpress/?wpsl_id=3
Here is code for that :
add_action("template_redirect", 'my_theme_redirect');
function my_theme_redirect() {
$dpath = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] ;
$templatefilename = 'single-store.php';
if (strpos($dpath,'wpsl_id') !== false){
$return_template = get_template_directory() .'/' .$templatefilename ;
//$nurl = get_permalink($return_template);
//echo $return_template; exit;
wp_redirect($return_template);
exit;
}
}
This code is not redirecting me to any-where.It stays on the same page localhost/wordpress/?page_id=391 What might be the issue?
Anyone can suggest me how can I direct to URL when it detects wpsl_id in the URL ? I want to redirect them to plugin's template folder where I have added a file for that.
EDITED :
I have added above code in the plugin file just above the short code. Hope I has nothing to do with this issue :)

The template name looks fine. You just need to register the template before you call it.
Add this filter so this will execute before your action:
add_filter( 'template_include', 'single-store' );

Related

wp_login_url() returns relative URL instead of full URL

Trying to debug an issue on my WordPress site where using wp_login_url() both in templates and emails doesn't return the full URL.
It returns "/login/" instead of "https://sitename.com/login"
Any idea how to fix this?
I think something changed your login url if your not getting wp-login.php.
Anyway you can add anything after your site url by using this function
site_url('anything')
for your url
site_url(wp_login_url())
or (I prefer this)
site_url('login')
For adding filter to your login url , you can use this code (I don't suggest this way)
function filter_login_url( $login_url ) {
return site_url( $login_url );
}
add_filter( 'login_url', 'filter_login_url', 10, 3 );

Pretty URLs for named anchors on WordPress page

I'm working on a WordPress site in which the services page is one long page with anchors for each section.
www.mysite.com/services/#human-resources jumps to the Human Resources section as expected.
Is there any way in the .htaccess file to make a url such as www.mysite.com/services/human-resources/ jump directly to the anchor?
I've tried several different rewrite rules, the closest I've gotten is:
RewriteRule services/human-resources services/#human-resources [NE,L,R=301]
But this shows the # in the url which defeats the purpose. Removing the R=301 just causes it to do nothing.
Thanks.
I have been working on a similar site and this is how I solved that issue.
First add this action to functions.php, witch redirects the user to the proper anchor on the page.
function url_handler(){
$path = substr($_SERVER['REQUEST_URI'], 1); //get the path minus the '/'
if( is_page() || is_404() ){ // do you own conditional tags
wp_redirect( home_url("#" . $path) ); //redirect to the #anchor
exit();
}
}
add_action( 'template_redirect', 'url_handler' );
Next you need to make a javascirpt handler witch gets the url of the page (the url with the anchor) and make the proper event to scroll to that section. Something like this:
var hash = window.location.hash;
hash = hash.replace('#','');
hash = hash.replace('/','');
/* do stuff here */
Hope this helps

wordpress redirection from header.php not working

Nobody access my full wordpress website without login, if user is not logined than redirect it to http://example/submit-project/.
I'm trying to do this with this code:
$current_user = wp_get_current_user(); $crntusr = $current_user; if($crntusr->ID == 0){ wp_redirect( 'example.com/login'; ); }
But get this error:
Warning: Cannot modify header information - headers already sent by
(output started at
/home/content/n3pnexwpnas02_data02/36/3929936/html/wp-content/themes/freelanceengine/header.php:14)
in
/home/content/n3pnexwpnas02_data02/36/3929936/html/wp-includes/pluggable.php
on line 1195
1) wp_redirect() does not exit automatically, and should almost always be followed by a call to exit.
2) you should make an redirect before your template output something.
3) Better to refrain from using absolute links like http://example.com, you can get your WP login page via wp_login_url() function.
Remove your redirect code from your header.php file and try to add this code to your functions.php:
add_action ('wp_loaded', 'my_custom_redirect');
function my_custom_redirect() {
if (!is_user_logged_in() and !in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php')) ) {
wp_redirect(wp_login_url());
exit;
}
}
Update.
If your login form is on custom page (http://example/submit-project/), then you should use this code:
add_action ('wp_loaded', 'my_custom_redirect');
function my_custom_redirect() {
if (!is_user_logged_in() and $_SERVER['REQUEST_URI'] != '/submit-project/' ) {
wp_redirect('http://example/submit-project/');
exit;
}
}

Redirect outside WP after Login

I have http://mysite.com/admin.php
There I check wether the user is admin or not.
In second case I send the user to the wp-login page like this:
blog.mysite.com/wp-login.php?redirect_to=http%3A%2F%2Fmysite.com/admin.php
I expect redirect back for admin.php but wordpress always send me to wp-admin control panel.
I have researched.
When the dest. host is not in filter
allowed_redirect_hosts
WP just redirect the user to wp-admin.
How can I add more hosts to the filter?
If I put this example from the WP Codex on functions.php it stops working.
(http://codex.wordpress.org/Plugin_API/Filter_Reference/allowed_redirect_hosts)
add_filter( 'allowed_redirect_hosts' , 'my_allowed_redirect_hosts' , 10 );
function my_allowed_redirect_hosts($content){
$content[] = 'blog.example.com';
$content[] = 'codex.example.com';
// wrong: $content[] = 'http://codex.example.com';
return $content;
}
Add the following in your functions.php:
function my_allowed_redirect_hosts($allowed_host) {
$allowed_host[] = 'anothersite.com';
$allowed_host[] = 'www.someotherwebsite.com';
return $allowed_host;
}
add_filter('allowed_redirect_hosts','my_allowed_redirect_hosts');
Replace anothersite.com and add new values accordingly.
If you're trying to redirect users in a normal page, you can make use of Wordpress's wp_redirect() function:
<?php
wp_redirect( $location, $status );
exit;
?>
Documentation: wp_redirect()
Hope this helps!
Finally it works!
What I was doing wrong is putting the code in the WP functions.php file, and not in my custom theme functions.php file.
Thanks all!

Wordpress - How to include() a file that lies in another dir

I am writing a plugin that will take advantage of other plugin's features (think about a plugin for a plugin).
My file lies in /plugins/new-plugin/new-plugin.php
and I need to make a
include(/plugins/OLD_plugin/old-plugin.php)
so I can use a couple of functions from the old-plugin.php file.
What is the correct way to do this? I could maybe make the functions in old-plugin.php available globally, but I don't want to change the old-plugin.php file.
I've already tried several ways to do this, but none worked. The new-plugin will only show some info in an options page, not viewable for the general public and does not interact with any public page or post in my site.
I've already tried $_SERVER, WP_PLUGIN_DIR, WP_CONTENT_DIR, the absolute server path, relative paths and even some black magic, but nothing seems to work good.
With some of this solutions the plugin's options page shows good but the blog's pages do not render. With other solutions the inverse happens, and with some other solutions nothing even render, be it admin pages or blog's pages, all with errors regarding to file not found.
The new-plugin.php is as simple as
<?php
/*
WP Common Headers
*/
global $wpdb;
if ( ! defined( 'WP_CONTENT_DIR' ) )
define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
if ( ! defined( 'WP_PLUGIN_DIR' ) )
define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );
include '/server-absolute-path/public_html/gameblogs/wp-content/plugins/old-plugin/old-plugin.php';
add_action('admin_menu', 'new_plugin_menu');
function new_plugin_menu() {
$page_title = 'New Plugin';
$menu_title = 'New Plugin';
$function = 'new_plugin_admin_page';
$menu_slug = 'new_plugin';
add_menu_page($page_title, $menu_title, 0, __FILE__, $function);
}
function new_plugin_admin_page() {
$result = old_plugin_link_data(" WHERE link_destination NOT LIKE '/%' AND link_destination NOT LIKE '%gameblogs%'");
$total = count($result);
old_plugin_list_links($result, $total, FALSE, FALSE);
*/
}
?>
thanks for any ideas!
check the old plugin files and see if there are any do_actions or apply_filters in it. If there are then you can hook into the old plugin script with your new plugin using add_action and apply_filters and execute other things you want to do.
see http://codex.wordpress.org/Function_Reference/do_action
and http://codex.wordpress.org/Function_Reference/apply_filters
For example (very basic example):
If in old plugin you find a:
do_action('some_type_of_reference);`
In your new plugin you can hook into it by doing:
`add_action('some_type_of_reference', 'name_of_my_function');
function name_of_my_function() {
//executed code here
}`
If in old plugin you find a:
apply_filters('some_type_of_reference', $variable);
Then in your new plugin you can hook into the filter by doing:
apply_filter('some_type_of_reference', 'my_function');
function my_function( $variable ) {
//act on the variable from the filter.
return $variable;
}
Have you looked at the plugins_url function? I haven't had an in-depth read through your code, but it might help.
The plugins_url template tag retrieves the url to the plugins directory or to a specific file within that directory. You can hardcode the plugin slug in $path or pass FILE as a second argument to get the correct folder name.
Hope this helps!

Resources