Here's the thing...
I have created a button which on click opens a link like
https://www.something.com/something/?term=something
or [at first, I thought this is a URL problem... so I tried some other] Like:-
https://www.something.com/something.php?term=something
https://www.something.com/something?term=something
and else
now on this link, I want to echo $_GET['term']... which not happening, there is no error in the console... how to be able to echo it
I know the answer is in this document but I don't know how to use this thing... can anyone please describe the right way step by step...
https://codex.wordpress.org/Function_Reference/get_query_var
get_query_arg : Retrieve only public query variable in the WP_Query class of the global $wp_query object.
I propose for security , use rewrite class in Wordpress :
/**
* Add rewrite tags and rules
*
* #link https://codex.wordpress.org/Rewrite_API/add_rewrite_tag
* #link https://codex.wordpress.org/Rewrite_API/add_rewrite_rule
*/
/**
* Add rewrite tags and rules
*/
function myplugin_rewrite_tag_rule() {
add_rewrite_tag( '%city%', '([^&]+)' );
add_rewrite_rule( '^city/([^/]*)/?', 'index.php?city=$matches[1]','top' );
}
add_action('init', 'myplugin_rewrite_tag_rule', 10, 0);
for add to query arg :
/**
* Register custom query vars
*
* #param array $vars The array of available query variables
*
* #link https://codex.wordpress.org/Plugin_API/Filter_Reference/query_vars
*/
function myplugin_register_query_vars( $vars ) {
$vars[] = 'city';
return $vars;
}
add_filter( 'query_vars', 'myplugin_register_query_vars' );
example link in site : yoursite.com/city/45
get data in wordpress :
$city = get_query_var( 'city' );
echo $city;
That's quite simple
echo get_query_var( 'term', 'default_value');
Obviously you have to add this in functions.php for custom variables
function add_query_vars_filter( $vars ) {
$vars[] = "my_var";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
read further details here
Related
function autoset_parent_dropdown() {
global $pagenow;
if($pagenow == 'post-new.php') {
parent_dropdown( '477', '477', '0', null );
}
}
add_action( 'admin_init', 'autoset_parent_dropdown' );
How to pre select Page or Post Parent (ID) from Dropdown automatically?
on post-new.php, When creating new post.
I am trying above code but its not working. Its not selecting any parent post/page using parent_dropdown function.
I referred: https://developer.wordpress.org/reference/functions/parent_dropdown/
Please correct me if i am doing anything wrong.
Try this method. Tested on Wordpress 5.3.2, but should work on all versions.
add_filter('default_content', 'assign_parent_to_new_post', 10, 2);
/**
* #param string $post_content
* #param WP_Post $post
*
* #return string
*/
function assign_parent_to_new_post($post_content, $post)
{
if ($post->post_type != 'page') {
return $post_content;
}
$post->post_parent = 7; //Parent post_id goes here
wp_update_post($post);
return $post_content;
}
The idea behind it is pretty simple – hook somewhere inside blank post creation process, substitute post_parent and then push it to the database.
I am trying to make a custom URL like "mysite.com/artist/bruno-mars" with a custom template "artist.php", which is in the theme folder.
Here is the "artist.php" template code for example
<?php
get_header();
//my custom code goes here //
get_footer();
?>
And now in the head of the theme functions.php, I put the code
/**
* Custom rewrite
*/
add_action( 'init', 'ic_rewrite_rule' );
function ic_rewrite_rule() {
add_rewrite_rule( 'artist/([^&]+)', 'index.php?artist=$matches[1]', 'top' );
}
/**
* Custom query vars
*/
add_filter( 'query_vars', 'ic_register_query_var' );
function ic_register_query_var( $vars ) {
$vars[] = 'artist';
return $vars;
}
/**
* Custom page template
*/
add_action( 'template_redirect', 'ic_url_rewrite_templates' );
function ic_url_rewrite_templates() {
if ( get_query_var( 'artist' ) )
add_filter( 'template_include', function() { return get_template_directory() . '/artist.php'; });
}
But this is not working. It is showing 404 error. Can anybody help me out.
Here is an example - Custom rewrite rules in Wordpress but not sure, If that might work.
UPDATE
I figured it out. Here is code for functions.php in theme folder.
add_filter('query_vars', 'add_artistname_var', 0, 1);
function add_artistname_var($vars){
$vars[] = 'artistname';
return $vars;
}
add_rewrite_rule('^artist/([^/]+)/?$','index.php?pagename=artist&artistname=$matches[1]','top');
Due to some requirement I need to completely change the behavior of the get_comments_number();
A wp core function that returns the number of comments !
What I want is to change its behavior and instead of reading the comment I want it to override with the following function;
function get_review_numbers( $post_id = 0 ) {
$post = get_post( $post_id );
if ( ! $post ) {
$count = 0;
} else {
$count = get_rev_count($post->ID);
$post_id = $post->ID;
}
/**
* Filter the returned comment count for a post.
*
* #since 1.5.0
*
* #param int $count Number of comments a post has.
* #param int $post_id Post ID.
*/
return apply_filters( 'get_review_numbers', $count, $post_id );
}
Please guide me I want when ever I write some where get_comments_number it should execute the above function instead of executing its core function.
you can't overwrite functions in php. (theme devs test if function exists then create function to allow new functions to be created if run before that point)
However there is a filter in get_comments_number
return apply_filters( 'get_comments_number', $count, $post_id );
so you can filter the result with:
add_filter('get_comments_number', 'your_custom_funct');
function your_custom_funct($count){
$count++; // example you can add one to the count, etc
return $count; //return it when finished!
}
I am currently using the 's' parameter for my pages.
Wordpress automatically makes it a search.
I want to disable the 's' parameter. I dont need the search function on my site.
Tried to find an answer or a direction to go into, but no luck.
If you cant write it yourself there is a plugin for that, it unsets all the s parameters and the sets a 404:
<?php
/**
* Unsets all search-related variables in WP_Query object and sets the
* request as a 404 if a search was attempted.
*
* #param object $obj A WP_Query object
* #return null
*/
public static function parse_query( $obj ) {
if ( $obj->is_search && $obj->is_main_query() ) {
unset( $_GET['s'] );
unset( $_POST['s'] );
unset( $_REQUEST['s'] );
unset( $obj->query['s'] );
$obj->set( 's', '' );
$obj->is_search = false;
$obj->set_404();
}
}
?>
What is the difference between filter and hooks in wordpress.
How can i use the following filter in child theme ?
<?php
foreach ( $results as $result ) {
// external plugins can modify or disable field
$result = apply_filters( 'cp_package_field', $result, 'ad' );
if ( ! $result )
continue;
?>
how can i use the following hook in child theme ?
/**
* called in cp_add_new_listing() to hook into inserting new ad process
*
* #since 3.2.1
* #param int $post_id
*
*/
function cp_action_add_new_listing( $post_id ) {
do_action( 'cp_action_add_new_listing', $post_id );
}
For the first part of your question, here is a link to an explanation I found a while back that was pretty comprehensive and it helped me to understand the difference.
https://wordpress.stackexchange.com/questions/1007/difference-between-filter-and-action-hooks
To use the hook in your child theme, you'd probably need to have the following piece of code in your child theme's functions.php file:
/**
* Our callback function to the hook
* #param int $post_id id of the post
* #return void
*/
function my_child_theme_new_listing_cb( $post_id ) {
if($post_id == 10) { //Or whatever you want
echo 'Hello World';
}
//We do not have the responsibility to return something as it is a hook
}
add_action( 'cp_action_add_new_listing', 'my_child_theme_new_listing_cb', 10, 1 );
Hope it helps.