wordpress dynamic urls witout redirect - php

I need to create dynamic urls all loading same page (please note loading not redirecting) plugins that I could find only do redirects. Basically what I need is:
/somepage/something
/somepage/anotherthig
/somepage/thething/morethings
all loading existing page
/somepage
but the original url must be kept (not a redirect). Any advice on how t do it ( a plugin that does this works as well if you know of one) is greatly appreciayed.

Shouldn't be that hard and can be achieve by modifying $wp_query and $post global variable,
try this code
// modify variable by hooking it on 'wp' action
add_action( 'wp', function() {
global $wp, $wp_query, $post; //define global variable
//include $wp variable so you can check the url request
// list the url you want to use
$dynamic_url = [
'somepage/something',
'somepage/anotherthig',
'somepage/thething/morethings'
];
// check if page request is found from the array above
if ( in_array( $wp->request, $dynamic_url ) ) {
// build query argument
$args=[
'post_type' => 'page', //assuming its a page
'p' => 26 // page ID of the page you want to display on those dynamic URLS
];
// run the query and assign it to $wp_query global variable
$wp_query = new WP_Query( $args );
// modify is_single wp_query param and tell it its not a post
$wp_query->is_single = '';
// modify is_page wp_query param and tell it its a page
$wp_query->is_page = 1;
//assign (1st) found post to global post variable
$post = $wp_query->posts[0];
//modify header as 202 status (unless you want these pages to stay as 404), by defualt its a 404
status_header( 202 );
//done
}
});

You can use this plugin to create dynamic url's. this plugin is free.
https://wordpress.org/plugins/sdk-wp-dynamic-url/
if you are looking something advance then this plugin will surely do for you but its paid:
https://wordpress.org/plugins/if-so/
Hope that Helps
Happy Coding

Related

How to Create Author archive page for custom post types

My WordPress site uses one default "post" and "books" custom post. I have two different archive page design for different post types (ile. author.php and books-archive.php).
Now, I want to create a custom user profile page with two links, "All Posts by user" and "All books by user". My current user archive page looks like as follows;
xxxxxxx.com/author/nilanchala
Can someone help me how to create two author archive pages filtered by post types? One for "Post" and other for "Books"?
Please do not suggest any plugin.
This is just an example and you should modify it the way you want, we will be using a custom query and rewrite rule to build the url
First thing you need to do is to create rewrite rule for the two custom query you want to display.
example, you have to reset the permalink in order for the new rewrite rule to take effect,
this is better to be created in a class and in a custom plugin so you can simply call flush_rewrite_rules() function
during plugin activation to reset the permalink.
function _custom_rewrite() {
// we are telling wordpress that if somebody access yoursite.com/all-post/user/username
// wordpress will do a request on this query var yoursite.com/index.php?query_type=all_post&uname=username
add_rewrite_rule( "^all-post/user/?(.+)/?$", 'index.php?query_type=all_post&uname=$matches[1]', "top");
}
function _custom_query( $vars ) {
// we will register the two custom query var on wordpress rewrite rule
$vars[] = 'query_type';
$vars[] = 'uname';
return $vars;
}
// Then add those two functions on thier appropriate hook and filter
add_action( 'init', '_custom_rewrite' );
add_filter( 'query_vars', '_custom_query' );
Now that you have build a custom URL you can then load a custom query on that custom url by creating a custom .php file as template and using template_include filter to load the template if the url/request contains query_type=all_post
function _template_loader($template){
// get the custom query var we registered
$query_var = get_query_var('query_type');
// load the custom template if ?query_type=all_post is found on wordpress url/request
if( $query_var == 'all_post' ){
return get_stylesheet_directory_uri() . 'whatever-filename-you-have.php';
}
return $template;
}
add_filter('template_include', '_template_loader');
You should then be able to access yoursite.com/index.php?query_type=all_post&uname=username or yoursite.com/all-post/user/username
and it should display whatever you put on that php file.
Now that you have the custom url and custom php file, you can start creating your custom query inside the php file to query post type based on user_nicename/author_name,
e.g.
<?php
// get the username based from uname value in query var request.
$user = get_query_var('uname');
// Query param
$arg = array(
'post_type' => 'books',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'author_name' => $user;
);
//build query
$query = new WP_QUery( $arg );
// get query request
$books = $query->get_posts();
// check if there's any results
if ( $books ) {
echo '<pre>', print_r( $books, 1 ), '</pre>';
} else {
'Author Doesn\'t have any books';
}
I'm not sure why you need to build a custom query for all post as the default author profile loads all the default posts.

Wordpress permalink issue - advanced search

I am having some issue on my wordpress site.
The site url is http://www.sharedmachine.in/.
On the home page there is search option.
Now the permalink structure right now is like this - http://www.sharedmachine.in/?p=123.
Now after clicking the search button, it suppose to redirect to the 'advanced search' page. but it doesn't.
So I changed the permalink structure to custom - /%postname%/.
Still it doesn't work.
The URL it should redirect to is
http://www.sharedmachine.in/advanced-search/?filter_search_type (some filters)
But instead it shows-
http://www.sharedmachine.in/?filter_search_type (filters)
The page name is not coming in the URL.
Now the code to get url is -
$adv_submit=wpestate_get_adv_search_link();
and the function -
function wpestate_get_adv_search_link(){
$pages = get_pages(array(
'meta_key' => '_wp_page_template',
'meta_value' => 'advanced_search_results.php'
));
if( $pages ){
$adv_submit = esc_url ( get_permalink( $pages[0]->ID) );
}else{
$adv_submit='';
}
return $adv_submit;
}
If I change the line
$adv_submit=wpestate_get_adv_search_link();
To
$adv_submit='http://www.sharedmachine.in/index.php/advanced-search/';
it works.
What can be the issue here?
aparently the $pages variable never gets populated. i would try something like:
$page = get_page_by_path('advanced-search');
if ($page) {
$adv_submit = esc_url ( get_permalink( $page->ID) );
} else {
// do something else
}
in this case you would be looking up the page by its slug as opposed to a page-meta you may or may not have set

How to set up my Wordpress template structure for handling my custom post types

I'm having trouble setting up my Wordpress template. I'm not sure how to achieve the following. My website setup is the following:
I have a Custom Post Type "Chapter". A "Chapter" is parent to other CPT's. I have a few post types such as: reviews, interviews, blogposts, ...
On a chapter page I do a different WP_Query for every post type within this chapter.
page template single-chapter.php
Now I want to be able to click on "Blogpost (2)" and open an archive page of all blogpost within the current chapter. How can I achieve this? I assume I should create a template page "archive-blogpost.php". I already found that I can link to this page using:
<?php echo get_post_type_archive_link( 'blogpost' ); ?>
However I can't see how this page could know in what chapter i'm currently in?
I never used the WP Types plugin so far, but here is what you could do: add a chapter parameter to the archive link (with a rewrite rule for it), that you'll get in your blogposts archive template and display the posts conditionally to this parameter.
First we change the archive link to send the chapter slug:
<?php echo get_post_type_archive_link( 'blogpost' ) . '/' . $post->post_name; ?>
Then we define in functions.php this new rewrite tag:
function custom_rewrite_tag() {
add_rewrite_tag('%chapter%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
To make the URL looks good, we'll add a rewrite rule (so you don't have a url like /archive/?chapter=chapter-1, but /archive/chapter-1) - this still go to functions.php:
function custom_rewrite_rule($rules) {
add_rewrite_rule('^archive/([^/]+)/?$', 'index.php?post_type=blogposts&chapter=$matches[1]', 'top');
}
add_filter('init', 'custom_rewrite_rule', 10, 0);
You may have to change the URL / post type name depending of your configuration.
And last, you can get this query arg in your blogposts archive template with $wp_query->query_vars array:
$wp_query->query_vars['chapter']
Since I don't know much about WP Types, I'm not really sure about what follows, but it seems you could query the childs posts of the chapter with this:
if(isset($wp_query->query_vars['chapter']) && $chapter = get_page_by_path($wp_query->query_vars['chapter'])) {
$childargs = array(
'post_type' => 'blogposts',
'numberposts' => -1,
'meta_query' => array(array('
key' => '_wpcf_belongs_property_id', 'value' => $chapter->ID
))
);
$child_posts = get_posts($childargs);
} else {
// default template : display all posts
}
I used a WP types functions to achieve what I wanted:
$child_posts = types_child_posts("blogpost", array('post_id' => $_GET['wpv-pr-child-of']));
With this I can query all the child posts from the custom post type on a page.
I also use a session variable to keep track of the current chapter I'm in.

Is it possible to define shortcode and then cal in template file - Wordpress

I have a file a.php where I have defined link
I have created a custom template custom.php and created a page at wordpress backend that has page_id=11
So, from a.php I am sending a user to custom.php with the help of page_id and then fetching the key=123
This is working fine..
The only problem is that I am not sure about page_id = 11.
Is it possible to get the same using the shortcode so as I can avoid using the page_id in my template file.
Function to create a shortcode to get the page id
function id_shortcode( $atts ) {
// extract the variables, if there is no ID attr, the default is 1
extract( shortcode_atts( array(
'id' => 1
), $atts ) );
// set page id to either the id attr which defaults to one.
$page_id = $id;
$page_data = get_page( $page_id );
return
}
// shortcode registration
add_shortcode( 'getid', 'id_shortcode' );
Then you can simply use [getid] shortcode on your page to get that page id
I hope i have understood and solved your problem...
And you can call this shortcode also in your template file
<?php echo do_shortcode("[getid]"); ?>

How to pass extra variables in URL with WordPress

I am having trouble trying to pass an extra variable in the url to my WordPress installation.
For example /news?c=123
For some reason, it works only on the website root www.example.com?c=123 but it does not work if the url contains any more information www.example.com/news?c=123. I have the following code in my functions.php file in the theme directory.
if (isset($_GET['c']))
{
setcookie("cCookie", $_GET['c']);
}
if (isset($_SERVER['HTTP_REFERER']))
{
setcookie("rCookie", $_SERVER['HTTP_REFERER']);
}
Any Ideas?
To make the round trip "The WordPress Way" on the "front-end" (doesn't work in the context of wp-admin), you need to use 3 WordPress functions:
add_query_arg() - to create the URL with your new query variable ('c' in your example)
the query_vars filter - to modify the list of public query variables that WordPress knows about (this only works on the front-end, because the WP Query is not used on the back end - wp-admin - so this will also not be available in admin-ajax)
get_query_var() - to retrieve the value of your custom query variable passed in your URL.
Note: there's no need to even touch the superglobals ($_GET) if you do it this way.
Example
On the page where you need to create the link / set the query variable:
if it's a link back to this page, just adding the query variable
<a href="<?php echo esc_url( add_query_arg( 'c', $my_value_for_c ) )?>">
if it's a link to some other page
<a href="<?php echo esc_url(
add_query_arg( 'c', $my_value_for_c, site_url( '/some_other_page/' ) )
)?>">
In your functions.php, or some plugin file or custom class (front-end only):
function add_custom_query_var( $vars ){
$vars[] = "c";
return $vars;
}
add_filter( 'query_vars', 'add_custom_query_var' );
On the page / function where you wish to retrieve and work with the query var set in your URL:
$my_c = get_query_var( 'c' );
On the Back End (wp-admin)
On the back end we don't ever run wp(), so the main WP Query does not get run. As a result, there are no query vars and the query_vars hook is not run.
In this case, you'll need to revert to the more standard approach of examining your $_GET superglobal. The best way to do this is probably:
$my_c = filter_input( INPUT_GET, "c", FILTER_SANITIZE_STRING );
though in a pinch you could do the tried and true
$my_c = isset( $_GET['c'] ) ? $_GET['c'] : "";
or some variant thereof.
There are quite few solutions to tackle this issue. First you can go for a plugin if you want:
WordPress Quickie: Custom Query String Plugin
Or code manually, check out this post:
Passing Query String Parameters in WordPress URL
Also check out:
add_query_arg
Since this is a frequently visited post i thought to post my solution in case it helps anyone. In WordPress along with using query vars you can change permalinks too like this
www.example.com?c=123 to www.example.com/c/123
For this you have to add these lines of code in functions.php or your plugin base file.
From shankhan's anwer
add_filter( 'query_vars', 'addnew_query_vars', 10, 1 );
function addnew_query_vars($vars)
{
$vars[] = 'c'; // c is the name of variable you want to add
return $vars;
}
And additionally this snipped to add custom rewriting rules.
function custom_rewrite_basic()
{
add_rewrite_rule('^c/([0-9]+)/?', '?c=$1', 'top');
}
add_action('init', 'custom_rewrite_basic');
For the case where you need to add rewrite rules for a specifc page you can use that page slug to write a rewrite rule for that specific page. Like in the question OP has asked about
www.example.com/news?c=123 to www.example.com/news/123
We can change it to the desired behaviour by adding a little modification to our previous function.
function custom_rewrite_basic()
{
add_rewrite_rule('^news/([0-9]+)/?', 'news?c=$1', 'top');
}
add_action('init', 'custom_rewrite_basic');
Hoping that it becomes useful for someone.
add following code in function.php
add_filter( 'query_vars', 'addnew_query_vars', 10, 1 );
function addnew_query_vars($vars)
{
$vars[] = 'var1'; // var1 is the name of variable you want to add
return $vars;
}
then you will b able to use $_GET['var1']
<?php
$edit_post = add_query_arg('c', '123', 'news' );
?>
Go to New page
You can add any page inplace of "news".
One issue you might run into is is_home() returns true when a registered query_var is present in the home URL. For example, if http://example.com displays a static page instead of the blog, http://example.com/?c=123 will return the blog.
See https://core.trac.wordpress.org/ticket/25143 and https://wordpress.org/support/topic/adding-query-var-makes-front-page-missing/ for more info on this.
What you can do (if you're not attempting to affect the query) is use add_rewrite_endpoint(). It should be run during the init action as it affects the rewrite rules. Eg.
add_action( 'init', 'add_custom_setcookie_rewrite_endpoints' );
function add_custom_setcookie_rewrite_endpoints() {
//add ?c=123 endpoint with
//EP_ALL so endpoint is present across all places
//no effect on the query vars
add_rewrite_endpoint( 'c', EP_ALL, $query_vars = false );
}
This should give you access to $_GET['c'] when the url contains more information like www.example.com/news?c=123.
Remember to flush your rewrite rules after adding/modifying this.
to add parameter to post urls (to perma-links), i use this:
add_filter( 'post_type_link', 'append_query_string', 10, 2 );
function append_query_string( $url, $post )
{
return add_query_arg('my_pid',$post->ID, $url);
}
output:
http://yoursite.com/pagename?my_pid=12345678
This was the only way I could get this to work
add_action('init','add_query_args');
function add_query_args()
{
add_query_arg( 'var1', 'val1' );
}
http://codex.wordpress.org/Function_Reference/add_query_arg
In your case, Just add / after url and then put query arguments. like
www.example.com/news/?c=123 or news/?c=123
instead of
www.example.com/news?c=123 or news?c=123

Categories