can't use get_query_var when including template in wordpress - wordpress

I am using this function (in Wordpress's functions.php file) to redirect a specific url to a specific php script file:
add_action('init', function() {
$url_path = trim(parse_url(add_query_arg(array()), PHP_URL_PATH), '/');
if ( $url_path === 'manoa' ) {
$load = locate_template('inc/manoa.php', true);
if ($load) {exit();}
}});
(i know i can use rewrite rules, but i rather use that for a few reasons).
it works great. except i can't use get_query_var on that page. any ideas how to fix it?

I think you should use a different add_action rather than init.
Try to use add_action('wp') instead.

Related

White page by using the filter template_include

I working on my plugin and tried to override some templates.
If I visit the page portfolio my screen gives a whitepage.
This is my code
define("PLUGIN_DIR_PATH", plugin_dir_path(__FILE__));
add_filter( 'template_include', 'plugin_tweak_template', 99);
function plugin_tweak_template( $template ) {
if ( is_page('portfolio')) {
$template = PLUGIN_DIR_PATH . 'required/templates/portfolio.php';
}
return $template;
}
I use this code in my plugin root file.
I think the define path has a conflict.
define("PLUGIN_DIR_PATH", plugin_dir_path(__FILE__));
PLUGIN_DIR_PATH is a example in many tutorials but you can define this one time.
Is you have another plugin check the define name from this plugins if this is the same name you have a conflict.
Remember always: use variable names etc. by your own and prevent issues.

How can I add no-follow to all links in a WordPress site in one go?

I'm coding a WordPress website in which I want all the external links to be "no-follow" by default.
It is quite long to go and edit each single link to add the "no-follow", is there a way to code it once for all?
Thank you
IMHO, the best way to do so is using a custom filter. Being generated dinamically by PHP, you can edit links on post and pages server-side even before a user visit them: WordPress used to add rel="nofollow" to each link by default in the past, but I see it doesn’t happen anymore. Then, I found a solution by Debjit Saha you may try on functions.php.
add_filter('the_content', 'my_nofollow');
add_filter('the_excerpt', 'my_nofollow');
function my_nofollow($content) {
return preg_replace_callback('/<a[^>]+/', 'my_nofollow_callback', $content);
}
function my_nofollow_callback($matches) {
$link = $matches[0];
$site_link = get_bloginfo('url');
if (strpos($link, 'rel') === false) {
$link = preg_replace("%(href=\S(?!$site_link))%i", 'rel="nofollow" $1', $link);
} elseif (preg_match("%href=\S(?!$site_link)%i", $link)) {
$link = preg_replace('/rel=\S(?!nofollow)\S*/i', 'rel="nofollow"', $link);
}
return $link;
}
Please, notice that some plugins could break this code and I’ve never tried this on newer WordPress versions.
One way you can do it is using JavaScript:
var hrefToCheck = "mysite.com" // change this string
noFollowExternalLinks(hrefToCheck); // call to run the function below
function noFollowExternalLinks(siteHref) {
document.querySelectorAll('a').forEach(function(link) {
if (!link.href.includes(siteHref)) {
link.setAttribute("rel", "nofollow");
}
})
}
So, you can change hrefToCheck variable to a piece of your site URL that will be checked inside each link inside the page.
The function will loop to check every link in the page and apply rel="nofollow" to all external links (that don't match the text in the variable).

How to redirect based on URL

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' );

Can't create rewrite rule

I try to create rewrite rule for my custom post type. I need it to be something like "/%post_type_name%/%taxonomy_slug/%post_slug%". Here's the code I used to add rule:
add_action('init', 'episodes_rewrites_init');
function episodes_rewrites_init(){
add_rewrite_rule(
'^episodes/([^/]*)/([^/]*)/?$',
'index.php?post_type=episodes&seasons=$matches[1]&episodes=$matches[2]', 'top'
);
}
It doesn't work and i can't figure out why. Please help.
Well mate, I have never used add_rewrite_rule, WP codex says that is It is most commonly used in conjunction with add_rewrite_tag().
So just I show you how veterans used to do this rewriting rules in the old times.
But moreover If you are building a plugin for the masses you must avoid use WP 3.5 features Much people still use WP2.*.
These set of actions can help you:
add_action('init', 'flushRewriteRules');
function flushRewriteRules()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_filter('rewrite_rules_array', 'insertMyRewriteRules');
function insertMyRewriteRules($rules)
{
$newrules['^episodes/([^/]*)/([^/]*)/?$'] = 'index.php?post_type=episodes&seasons=$matches[1]&episodes=$matches[2]';
return $newrules + $rules;
}
add_filter('query_vars', 'insertMyRewriteQueryVars');
function insertMyRewriteQueryVars($vars)
{
array_push($vars, 'episodes', 'seasons', 'post_type');
return $vars;
}
Well, I've just installed plugin called "Custom Post Type Permalinks", added url template for my custom post like "/%seasons%/%postname%/" and now it rewrites urls like I want. But I still don't know how to do it in code though...

Wordpress Plug-ins: How-to add custom URL Handles

I'm trying to write a Wordpress Plug-in but can't seem to figure out how you would modify how a URL gets handled, so for example: any requests made for:
<url>/?myplugin=<pageID>
will get handled by a function in my plug-in. I'm sure this is a very simple to do, but I'm pretty new to working with Wordpress and couldn't find it in the documentation.
In order to handle just a specific URL use the code below:
add_action('parse_request', 'my_custom_url_handler');
function my_custom_url_handler() {
if(isset($_GET['myplugin']) && $_SERVER["REQUEST_URI"] == '/custom_url') {
echo "<h1>TEST</h1>";
exit();
}
}
add_action('parse_request', 'my_custom_url_handler');
function my_custom_url_handler() {
if( isset($_GET['myplugin']) ) {
// do something
exit();
}
}
That should set you on the right direction. parse_request happens before WordPress runs any of the complicated WordPress queries used to get the posts for the current URL.

Resources