I have developed plugin and uses shortcode to display.
So, there is two type of items to display like
if get title in url, display single item and if not all items so i have added into if else in condition.
So for that title, i have passed
add_query_arg( array('title' =>sanitize_title($data->get_title())), get_permalink($post_id) );
now url is generated by this is domain.com/post-slug/?title=nameoftitle
but i want to url like domain.com/post-slug/nameoftitle
I have tried following code but not working
function update_rewrite_rules() {
add_rewrite_tag( '%title%', '([^/]*)' );
add_rewrite_rule(
$newVarRegex,
'index.php?pagename=$matches[1]&title=$matches[2]',
'top'
);
}
add_action( 'init', 'update_rewrite_rules' );
but its not working
I got solution of it. working code is:
function js_update_rewrite_rules() {
add_rewrite_tag( '%title%', '([^/]*)' );
$rewrite_rules = get_option( 'rewrite_rules' );
$newVarRegex = '^([^/]*)/title/([^/]*)/?';
// check if the rule exists
if ( !isset( $rewrite_rules[$newVarRegex] ) ) {
add_rewrite_rule(
$newVarRegex,
'index.php?pagename=$matches[1]&title=$matches[2]',
'top'
);
flush_rewrite_rules();
}
}
function js_on_activation() {
js_update_rewrite_rules();
}
register_activation_hook( __FILE__, 'js_on_activation' );
function js_on_init() {
js_update_rewrite_rules();
}
add_action( 'init', 'js_on_init' );
Related
I've a problem with creating Custom Page Template based on URL Rewrite.
This is my rewrite rule:
add_rewrite_rule( '([^/]+)/var1', 'index.php?category_name=$matches[1]&var1=yes', 'top' );
And this is how I adding custom template to this url:
if ( get_query_var( 'var1' ) ) {
include( get_template_directory() . '/cat-var1.php' );}
It works, but when I scroll down the page I see content from parent page in this case it's current category.
So page show content from page var1 and after it content from parent page, how to delete content of parent page?
This is my full code:
function prefix_movie_rewrite_rule() {
add_rewrite_rule( '([^/]+)/var1', 'index.php?category_name=$matches[1]&var1=yes', 'top' );}
add_action( 'init', 'prefix_movie_rewrite_rule' );
function prefix_register_query_var( $vars ) {
$vars[] = 'var1';
return $vars;
}
add_filter( 'query_vars', 'prefix_register_query_var' );
function prefix_url_rewrite_templates() {
if ( get_query_var( 'var1' ) ) {
include( get_template_directory() . '/cat-var1.php' );
}
}
add_action( 'template_redirect', 'prefix_url_rewrite_templates' );
Regards
I have custom search page, that have permalink http://mywebsite.com/custom-search/
What should I do to pass the search keyword as a parameter, like this: http://mywebsite.com/custom-search/keyword
I get error 404 page. Or may be there a way to change standard permalink /search/ to /custom-search/ ?
You should use rewrite endpoints
A sample code :
/*!
* URL rewrite
*/
function my_custom_rewrite_rules() {
$page_id = 123;
$page_path = get_page_uri( $page_id );
add_rewrite_endpoint( 'keyword', EP_PAGES );
add_rewrite_rule('^'. $page_path .'/(.*)/?', 'index.php?page_id=' . $page_id . '&keyword=$matches[1]', 'top');
}
add_action('init', 'my_custom_rewrite_rules');
and then add it as a query_var
function my_custom_query_vars($vars) {
if( isset( $_GET['keyword'] ) && !empty( $_GET['keyword'] ) ) {
$vars[] = 'keyword';
}
return $vars;
}
add_filter( 'query_vars', 'my_custom_query_vars', 10, 1 );
you will be able to retrieve the value of the passed keyword via get_query_var("keyword")
hope it helps
Note : You must update your permalinks structure or use flush_rewrite_rules(); after adding these codes
just changed search base with function
function vital_custom_search_base() {
$GLOBALS['wp_rewrite']->search_base = 'custom-search';
}
add_action( 'init', 'vital_custom_search_base' );
function only works after resave in settings > permalinks
The plugin I'm writng adds a custom url and handles it with a custom template using this:
public function url_for_calendar() {
add_rewrite_rule( 'calendario', 'index.php?upcoming-events=true', 'top' );
}
public function template_for_calendar( $path ) {
if ( get_query_var( 'upcoming-events' ) ) {
$template = get_template_directory() . '/upcoming-events.php';
return $template;
}
return $path;
}
public function upcoming_event_query_var( $vars ) {
$vars[] = 'upcoming-events';
return $vars;
}
add_action( 'init', array( $this, 'url_for_calendar' ) );
add_filter( 'template_include', array( $this, 'template_for_calendar' ) );
add_filter( 'query_vars', array( $this, 'upcoming_event_query_var' ) );
Things go as planned, but the problem I'm having is that on my header, where I have:
<body <?php body_class(); ?>>
body_class() ends up adding "home" to the list of classes. I read the docs for "is_home" and for "body_class", but I still don't understand why would it add the "home" class to this particular URL. Should I remove it (and add pertinent classes) with the body_class filter, or is there a better practice to accomplish this?
Using WP 4.1.
Actually, is_home() adds the class "blog".
The class "home" is added for the front page (is_front_page()).
If the settings are configured for a static front page and this code is run there, then you will get "home" as a body class.
I'm trying to get my Wordpress plugin to deactivate automatically after a simple check. It seems to be calling the admin_notices method just fine, but the deactivate_plugin() method does not do anything. This is in the class constructor:
// End if the theme isn't compatible
if ( FALSE == $this->themesupport['support'] ) { // This test works fine
add_action( 'admin_init', array( &$this, 'deactivate_plugin' ) ); // Plugin doesn't deactivate
add_action( 'admin_notices', array( &$this, 'admin_notices' ) ); // I get notices
if ( isset( $_GET['activate'] ) )
unset( $_GET['activate'] );
return;
} // if()
The method is pretty straightforward:
public function deactivate_plugin() {
deactivate_plugins( plugin_basename( __FILE__ ) );
} // deactivate_plugin()
Putting an echo in that deactivate_plugin method and it gets called. I've also tried including the plugins.php file from core with no change.
The following works:
<?php
/**
* Plugin Name: (SO) Self-deactivate with $_GET['my_deactivate']
*/
add_action( 'admin_init', function()
{
if( isset( $_GET['my_deactivate'] ) )
{
deactivate_plugins( plugin_basename( __FILE__ ), true );
$url = admin_url( 'plugins.php?deactivate=true' );
header( "Location: $url" );
die();
}
});
After activating, enter any admin URL adding ?my_deactivate, e.g.:
http://example.com/wp-admin/users.php?my_deactivate
Reference:
Function deactivate_plugins does not exist
I was calling deactivate_plugins() from within a class, rather than the plugin file itself, which of course returned the wrong location for FILE
I have a page template that gets some variables added to the end of the url so I can display data based on what was passed.
ie: mysite.com/search-listins/listing/?address=123+The+Street&mls-number=00000
I need to convert this into a pretty permalink. Somthing like this:
mysite.com/search-listings/listing/123-The-Street OR mysite.com/search-listings/listing/00000-123-The-Street
I tried using this function. But nothing seems to be working. Any thoughts? These are not coming from a custom post type. As you can see, these are not a custom post type. These are MLS items that live outside the wp_ tables.
Function:
function setup_filter_rewrites(){
add_rewrite_rule('search-listings/listing/([^/]*)/?', 'index.php?pagename=search- listings/listing/?address=$matches[1]&mls-number=$matches[2]', 'top');
}
add_action( 'init', 'setup_filter_rewrites' );
function setup_filter_query_vars( $query_vars ){
$query_vars[] = 'listing';
return $query_vars;
}
add_filter( 'query_vars', 'setup_filter_query_vars' );`
I'm always using this code instead of add_rewrite_rule() function:
add_filter( 'query_vars', 'my_query_vars' );
function dlouhavidea_query_vars( $vars ) {
$vars[] = 'address';
$vars[] = 'miles';
return $vars;
}
add_action( 'generate_rewrite_rules', 'my_rewrite_rules' );
function ,y_rewrite_rules( $wp_rewrite )
{
$wp_rewrite->rules = array(
'search-listings/listing/address/?([^/]*)/?$' => $wp_rewrite->index . '?pagename=search-listings&address=' . $wp_rewrite->preg_index( 1 ),
'search-listings/listing/miles/?([0-9]{1,})/?$' => $wp_rewrite->index . '?pagename=search-listings&miles=' . $wp_rewrite->preg_index( 1 )
) + $wp_rewrite->rules;
}
You'll than find your vars in get_query_var('address') and get_query_var('miles');