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

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.

Related

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).

Give access to only two (/home, /inbox) page for a particular user with specific role in wordpress

I want to give only two page (/home, /inbox) access to my user with role "Vendor", if user tries to access other pages than it will automatically redirect to "/inbox" page, I put the below code to achieve the functionality but after adding this to function.php, site again n again redirect and finally dies with message "Page isn't properly redirected". please suggest what is wrong with my tried code or any other solution.
function vendor_redirect() {
global $post;
if(current_user_can('Vendor') && !in_array($post->slug,array("home","inbox"))) {
wp_safe_redirect('/inbox');
}
}
add_action('template_redirect', 'vendor_redirect');
The main issue the way I tried to get the page slug, the correct way to get the slug is "$post->post_name". also I put exit after wp_safe_redirect as well because codex suggest that:
function vendor_redirect() {
global $post;
if(current_user_can('Vendor') && !in_array($post->post_name,array("home","inbox"))) {
wp_safe_redirect(get_permalink(get_page_by_path( 'inbox' )));
exit;
}
}
add_action('template_redirect', 'vendor_redirect');

Cant make my wordpress url_rewrite work

I am working on a simple wordpress plugin to manage events.
I have a link in my layout where I want to link to a event-page which is located in my plugin folder.
I dont want the long url like localhost/project/wp-content/plugins/event-manager/event-page.php?eventid=1.
I want it to be like localhost/project/event/1. Where the 1 is referred to the eventid get.
I've searched a lot and still I cant find the way to get it worked.
My code is as follow:
register_activation_hook(__FILE__, 'link_rewrite');
function link_rewrite() {
add_rewrite_rule('^event/^[0-9]', plugins_url('event-manager') . '/event-page.php? eventid=$matches[1]', 'top');
flush_rewrite_rules(false);
}
add_filter('query_vars', 'link_rewrite_query_vars');
function link_rewrite_query_vars($query_vars) {
$query_vars[] = 'eventid';
return $query_vars;
}
Hope someone have a solution.

modify url permalink wordpress

I use wordpress and I have the following question,
I use the URL:
- www.mysite.com/page
- www.mysite.com/cat/page
- www.mysite.com/custompostype/post
The value of permalinks is %category%/%postname%, everything works fine except now I have to use two languages ​​and I want to configure as follows:
www.mysite.com/EN/page
www.mysite.com/ES/cat/page
www.mysite.com/BR/custompostype/post
I want to change the structure of wordpress urls where the first rule is the language and to take it as variable value. Similar to /%lang%/%category%/%postname%/
i used the wp_rewrite and have had no success, any idea?
for now I just found a solution to my problem, which I handled sessions
<?php function init_sessions() {
if (!session_id()) {
session_start();
} } add_action('init', 'init_sessions'); ?> <?php function detecta_idioma() { if($_SESSION["IDIOMA"]["actual"]=="") {
$_SESSION["IDIOMA"]["actual"]="ES";
$_SESSION["IDIOMA"]["abreviatura"]="";
$_SESSION["IDIOMA"]["nombre"]="Español";
$_SESSION["IDIOMA"]["tag"]="ES"; } if($_GET["action"]=="idioma" &&
$_GET["lang"]) { unset($_SESSION["IDIOMA"]);
switch($_GET["lang"]) { case "EN":
$_SESSION["IDIOMA"]["actual"]="EN";
$_SESSION["IDIOMA"]["abreviatura"]="_en";
$_SESSION["IDIOMA"]["nombre"]="Ingles";
$_SESSION["IDIOMA"]["tag"]="EN"; break; case "ES": default:
$_SESSION["IDIOMA"]["actual"]="ES";
$_SESSION["IDIOMA"]["abreviatura"]="";
$_SESSION["IDIOMA"]["nombre"]="Español";
$_SESSION["IDIOMA"]["tag"]="ES"; break;
} } $IDIOMA=array("actual"=>$_SESSION["IDIOMA"]["actual"],"ab"=>$_SESSION["IDIOMA"]["abreviatura"],"nombre"=>$_SESSION["IDIOMA"]["nombre"],"tag"=>$_SESSION["IDIOMA"]["tag"]);
global $wp_rewrite;
$wp_rewrite->set_permalink_structure($_SESSION["IDIOMA"]["tag"].'/%category%/%postname%');
return $IDIOMA; } add_action('init', 'detecta_idioma'); ?>
works well on the route entries, eg www.misitio.com/en/post-demo, t not working with pages or categories or taxonomies, eg: www.misite.com/es/category/, www.site.com/es/page/subpage or www.site.com/br/taxonomy <----- Dont work, shows error 404 page
I can not use the plugin qTranslate and CMS plugin multulengual either they do not work well with the use of plugins Advanced custom fields and custom post types UI.
Thanks

WordPress redirect after successful form validation and submit

I have a custom page template with a form in page-report.php.
I do validation on it, so I need the action to lead to the same form, but I need to redirect to a different page on successful validation.
wp_redirect() is not working, because is spitting out the header() function after the output was started already.
if($_POST['report'])
{
if($validator->ValidateForm())
{
wp_redirect('http://thankyou') // redirect
}
}
I cannot use ob_start() and ob_flush() because the header is not included in this page template.
I tried to put a function in functions.php :
add_action('get_header','redirect_to');
function redirect_to($page){
if($page)
{
wp_redirect('http://www.google.com');
}
}
But that works only if I don't have the conditional if().
If I use it, the wp_redirect() is being spat out after the output was started.
What is my best approach to do this?
Thanks.
I think you have to use the save_post hook:
do_action('save_post', 'custom_add_save');
function custom_add_save($postID){
// called after a post or page is saved
if($_POST['report']) {
if($validator->ValidateForm())
{
wp_redirect('http://thankyou') // redirect
}
}
Also you could just try using a plugin instead of your own code...Gravity Forms and Contact form 7 both work well.
}
I got it...
Since I was doing everything from inside an admin page, the header was fired up before the wp_redirect() as it was explained in the question.
So I ended up making a new function at the top:
add_action('admin_init','redirect_to');
function redirect_to()
{
if ( isset($_REQUEST['action']) && 'adduser' == $_REQUEST['action'] ) {
wp_redirect($redirect);
die();
}
}
}
That is making sure that the redirect_to() function will be fired up before the header (on admin_init). Maybe not the most elegant solution, but it works perfect.
So in the case anybody is looking for "Redirect after post" in wordpress, this is how you do it:
wp_redirect($_SERVER['REQUEST_URI']);
Try this
if( $_POST['report'] ) {
if( $validator->ValidateForm() ) {
header( 'Location: http://thankyou' ) ;
}
}

Resources