pagination fails when including custom variable in permalinks in wordpress - wordpress

I'm a wordpress newbie here so please go easy on me.
I am working on a wordpress site and I've run into two problems I just can't seem to find the answer to anywhere. I have permalinks set up on my wordpress site, along with paging, so for example, www.mysite.com/links/page/2 sends you to the second page of links.
I just created a custom template that pulls in two different taxonomies with the same tag name. To do that I created a page called tags that uses my custom template and I have a custom link that goes to it with a structure of: www.mysite.com/tags/?tag=mytag This is ugly but it works for testing purposes.
The problem happens when I go to page two and the link is: www.mysite.com/tags/?tag=mytag/page/2 which doesn't work.
I don't want to mess up my current permalinks, but I want to add a new rule that a) stops the page from breaking, and b) actually looks good. I would like to have the link just be www.mysite.com/tags/mytag/page/2
After a ton of digging I was able to get what I thought was the right code. This is what I've added to my functions.php in my theme directory:
add_action( 'generate_rewrite_rules', 'my_tag_rewrite_rules' );
//add_filter( 'query_vars', 'my_tag_query_vars' );
function my_tag_query_vars( $vars )
{
// $vars[] = 'tag'; // might not need this because 'tag' should already be registered
}
function my_tag_rewrite_rules( $wp_rewrite )
{
$wp_rewrite->rules = array(
'tags/([^/]+)/page/?([0-9]{1,})/?$' => $wp_rewrite->index . 'tags/?tag=' . $wp_rewrite->preg_index( 1 ) . '&paged=' . $wp_rewrite->preg_index( 2 ),
'tags/([^/]+)/?$' => $wp_rewrite->index . 'tags/?tag=' . $wp_rewrite->preg_index( 1 )
) + $wp_rewrite->rules;
}
(I commented out the add_filter part because 'tag' was already a query variable wordpress recognized). From what I read it looked like all I needed to do was go into the wordpress dashboard and go to settings->permalinks and supposedly it would change the .htaccess. It didn't.
My first question, is: is my rewrite rule correct? and secondly, what else do I need to do to get wordpress to update the .htaccess file?
Thanks a ton for any help!

Related

WordPress: Hide all the pages and posts expect for some pages

I have a multisite installation and I'm using one site for my landing pages. I want to hide all the pages on that site expect the landing pages that you need to have the URL to access.
I searched for a plugin that could do that but didn't find one. Is there anything that we can do with .htaccess? I tried looking into this but just looking at .htaccess gives me a headache and I don't want to break something for the other sites as this is MS installation.
Any help would be appreciated.
You can redirect from sites u don't want to be visible into other sites. In order to do this u can use this plugin:
https://pl.wordpress.org/plugins/redirection/
or if u know some php u can use hook and redirect.
Write and adjust in functions.php code below:
function wolfie_redirect_sample() {
if(is_front_page()) { //in this place u can use any other conditional
exit( wp_redirect( site_url( '/this_is_ur_landing' ) ) );
}
}
add_action( 'template_redirect', 'wolfie_redirect_sample' );

How to add #anchor at the end of the Wordpress URL if the traffic source is coming from Google search

I have Wordpress and I would like that all visitors who access my site by searching in google (traffic source), go directly to a specific part of the page (focus).
To focus on this part, I need to include the anchor at the end of the url: #welcome-source-google
https://example.com/category/page-or-post/#welcome-source-google
I think I need to insert this code, probably in functions.php:
function organic_source_anchor() {
if (preg_match('/(www\\.)?google\\./', $_SERVER['HTTP_REFERER'])) {
wp_redirect( get_permalink( $postID ) . '#welcome-source-google' );
exit;
}
add_action( 'template_redirect', 'organic_source_anchor' );
I got this code by researching, but since I am not a programmer, I believe there are several errors or maybe I need to put the code out of functions.
A problem that happens is that I didn’t want to disable the cache, as this frame appears to all visitors, but if the source is from google, I wanted the focus on the part this page.

how to use filter or rewrite to change parameters to output desired permalink in child theme

i have asked my theme provider, i want to change 'TEAM-MEMBER' to 'AUTHOR'. As like follow
Now link is : http://www.domain.in/team-member/authorname
but i want make this : http://www.domain.in/author/authorname
my theme provider say that, Here is the code to update the team-member to author.
<?php
add_filter( 'pgscore_register_cpt_teams', 'pgscore_extend_cpt_teams' );
function pgscore_extend_cpt_teams( $args ){
// get current rewrite rules
$rewrite_new = $args['rewrite'];
// Change permalink slug to 'author'
$rewrite_new['slug'] = 'author';
// update rewrite rules with updated rewrite rules
$args['rewrite'] = $rewrite_new;
// return updated parameters
return $args;
}
?>
Add this code in the child theme (we assume that, you are using a child theme. If not, then create and use it).
We suggest using Child Theme for any customization. It can avoid losing any customization in the case of updating the theme.
but i have put follow code in function.php in child theme, showing this syntex, expected error.
this is screen shot of error
Please Remove <?php on line number 7. You have start unnecessary php under php file. remove <?php and try to save again I hope it working well.

How to generate a WordPress plugin install link

I'm created a plugin that replaces a previous plugin I wrote and adds some extra stuff too. I want to create an alert in the settings page of my old plugin with a link to download and install my new plugin and inform them that the old plugin is no longer supported by its developer. Currently, from the plugin page of my admin panel, install links look like this:
http://www.example.com/wp-admin/network/update.php?action=install-plugin&plugin=wptouch&_wpnonce=a8d64aa295
It seems to me there are at least two things too keep in mind when creating the URI:
Check whether or not this is a multi-site setup
Make sure I generate the correct nonce.
Does anybody know how to properly do this? Is there anything else I need to consider when generating this URI?
Thanks in advance for your help.
I've found an answer. In wp-admin/import.php, there's a great example of how this work.
Here is my implementation:
$plugin_name = 'my_plugin_name';
$install_link = 'Install ' . $plugin_name . '';
Then just output that link tag anywhere in your admin pages.
If you want to get fancy and add the thickbox modal window effect, just use the function add_thickbox() and attach it to an action that's called in a typical admin request, such as:
add_action('admin_menu', 'my_plugin_add_thickbox');
function my_plugin_add_thickbox() {
add_thickbox();
}
That should do it!
$action = 'install-plugin';
$slug = 'akismet';
wp_nonce_url(
add_query_arg(
array(
'action' => $action,
'plugin' => $slug
),
admin_url( 'update.php' )
),
$action.'_'.$slug
);
Use this. I found this and I am sure it will work.

How do I create a new page from my Wordpress plugin

In my wordpress plugin, I want to generate a page on the fly.
I could have the user create the page for me. But I would rather not have them do any steps. Just let them activate it and it works.
So I was wondering is there a way for me to do this, which maintains all the functionality within the plugin.
My initial idea was to add a rewrite rule
add_rewrite_rule('my_page/$', 'wp-content/plugins/my_plugin/page.php', 'top');
Then in my plugin I can have a page.php. Which works well, but I cannot get the header/footer etc.
I am very new to wordpress, so chances are i am missing somethign obvious.
You could create a 404 page code snippet that does wp_insert_post() and then redirects the user to it.
Your theme's 404.php would look like this:
<?php
$post_id = wp_insert_post("post_title" => "my post title", "post_content" => "Lol");
header("location:" . get_permalink( $post_id ) );
die();
?>

Resources