Where is Wordpress search parameter checked? - wordpress

This is going to sound stupid but I am stymied. As I've learned a search form is recognised by Wordpress when the name of the input is gives as 's'. The action is always the home url. But nowhere in index.php or page.php(depending on your homepage setting) does it check for the parameter 's'. Where exactly does Wordpress check for this parameter?
I am trying to output the search result on a particular page but it does not work when I set the 'action' value to anything but the Home URL. I am hoping I can gain further understanding on how search works and maybe implement the same parameter checking in my desired page
Howdy_McGee check this out

WordPress doesn't require that search goes back to the homepage. The s query var is a reserved keyword for WordPress search. What you can do is modify your search form to include the current url:
global $wp;
<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( $wp->request ) ); ?>">
By doing so on a term page ( taxonomy archive ) it will also restrict the search to that specific term without any additional work. You can modify this in your searchform.php template. If you don't have one you can grab it from the 2017 theme files.
WordPress parse most requests through the class-wp-query.php file handed by the WP_Query Class/Object. Search is handled by WP_Query::parse_search()
If you're looking to modify the search or really any queries you can always use pre_get_posts to change the query object before it's requested from the database. If you want to modify the SQL before it's requested you can use the posts_clauses hook, usually this is used for incorporating custom tables.

Related

Wordpress Search URL messing up for posts

I set up my search in header.php, and on the main site search works properly. When I search from within a story, however, I never can get to the search results page because it tacks on the /?s=searchterm in the URL after the post slug. How can I circumvent this issue?
What have you provided in the action of your search form?
Use action="<?php echo home_url( '/' ); ?>"
I think this will solve your problem. If not provide your site url so that it would be easier to trace actual problem.

Whati is the diffrence between bloginfo and template_directory_uri?

I want to clear my concept that what is the difference between blog_info() and template_directory_uri() function? Please in details would be helpful!
Thanks
As you can see in the source, get_bloginfo( 'template_directory' ) and get_bloginfo( 'template_url' ) are simply wrappers for get_template_directory_uri():
case 'template_directory':
case 'template_url':
$output = get_template_directory_uri();
break;
IF you check with these links blog_info and template_directory_uri links you can clearly understand what is the difference between these two functions.
Anyway let me explain this for you
template_directory_uri
This function provides you the complete url to the theme directory you are currently using. Suppose if you are using theme x in your wordpress front-end, then when you call template_directory_uri() function it will return http://yourdomainname.com/wp-content/themes/x. This essentialy means that this function returns template directory URI for the current theme being used.
bloginfo
What this function does is that it will return all the information associated with your site which are set in the admin general settings and admin user profile. This function gives you information about the site url, admin email , site name, site description and lot of things. Most of these are available in the General Settings menu in the admin back-end. The bloginfo function accepts an input parameter. If you do not pass any input parameter by default it will show the Site title which is set in the admin back end. You can pass various other inputs like description, url, charset, version etc. These will give the info associated with them. So what bloginfo gives us is, it will provide the information regarding the site.
From bloginfo function we can get template_uri too, just do bloginfo('template_url');
If you prints out both these functions in your php page in wordpress theme, you can clearly found out what is the difference between these two functions. May be go to your index.php and just print out these two:
echo get_template_directory_uri();
bloginfo('name');
Hope this helps you

custom taxonomy terms page not found Wordpress

I’m getting a Page Not Found message when clicking on the link to display the terms in a taxonomy
Scenario:
I have a custom post type called "glossary"
Attached is a custom taxonomy called "section" with the rewrite set to “library/glossary-start-here”
I created a page that lists the terms in the taxonomy. This is a Wordpress page with the slug of “glossary-sections” - the template for the page is set to “taxonomy-section.php”. The page is a child of “library” and so the permalink for the page is “example.com/library/glossary-starts-here/glossary-sections/"
The template “taxonomy-section.php” has the following code:
<?php // Begin header section.
$argterms=array(
'include' =>array(
117,118,115)
);
$terms = get_terms('section', $argterms);
echo '<ul>';
foreach ($terms as $term) {
echo '<h3>' . $term->name . '</h3>';
}
echo '</ul>'; ?>
<div>
<?php
When clicking on the link to the page “glossary-sections”, which is used on a number of other pages, I get a page not found message.
I know the template does its job because on some occasions as I've poked around troubleshooting I've had it working. However, I can't find a consistent condition that causes the page to work. I suspect that the problem has something to do with the rewrite but my tests have been inconclusive.
I’d appreciate any suggestions on why this isn’t working and how to correct it.
I’m particularly puzzled by the fact that I can specify a page like “glossary-section” and yet have it not be found.
Thanks.
As I understand it, WordPress has a hierarchy / parse order (so to speak) as to what it looks for when it gets a request. Technically, a taxonomy is just a tag.
I'm not sure if this will help:
http://justintadlock.com/archives/2009/06/04/using-custom-taxonomies-to-create-a-movie-database
But J.Tadlock is always a solid place to start.
The source of the problem was that I had included a rewrite for the taxonomy, while at the same time using pages with assigned templates.
Once the rewrite was removed it appears that everything worked correctly.

Wordpress search results on external page

I have a custom search engine on a non-wordpress page. This search engine searches a database for a city (specified by a text input) and then returns the relevant information about that city and displays it on the page.
What I want to do now, is to below those results, display the results of a wordpress search of the same term on a blog that resides on the same server/domain. So basically I want to show the same results that a wordpress search of that keyword would return, but I want to display them on a non-wordpress page, not in the blog's theme.
What I have:
a variable holding a search term, the search term has already been shampooed and conditioned to be search engine friendly.
What I don't want to do
I don't want to use an iframe and have the blog template be displayed on the page.
I can't have a secondary search box. I need to somehow use the value of the variable that I have, I can't ask the user to submit a second form or anything.
My ideas so far
Is there a way to run the wordpress search function and grab the data that it returns? I've gotten as far as including wp_blog_header.php on my static page so that I can make use of the wordpress functions.
Or would it be better to write a function that duplicates what the wordpress search does on the wordpress databases, but returns the data in a way that I can use?
Or is there a different approach that I should take for this that I've overlooked? Thanks!
You must first include the Wordpress files
<?php define('WP_USE_THEMES', false);
require('/server/path/to/your/wordpress/site/htdocs/blog/wp-blog-header.php');
then use query_posts and the loop
query_posts('s='.keyword);
while (have_posts()): the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<p>Read more...</p><?php
endwhile;

How to map an arbitrary URL to a function in a Wordpress plugin

I'm trying to create a Wordpress plugin that redirects visitors of example.com/redirect/XXX to a different page based on the value of XXX. I think I know how to do the redirect logic, but I don't know how to make sure that my Wordpress plugin function will be called when a visitor goes to example.com/redirect. Right now I just get a 404. There are other solutions that involved changing the .htaccess file, but I want this to function as a standalone plugin. Thanks!
When I need this kind of things i just create a common page with template as the "plugin", a page with all the functions that I need.
For example, if i need a shopping cart, I just create a cart.php as:
<?php
/*
Template Name: Cart
*/
// functions here
?>
And I go to my wp-admin and create a page with Cart as its template.
Depending on what exactly you want to do which is quite vague ( when you say page you actually mean page ? post ? cpt ? and when you say plugin functions - what are they ? and do you use permalinks ?)
.. but under some conditions you could use wp conditionals .
example ( from codex )
is_singular( 'foo' )
// Returns true if the post_type is "foo". execute plugin hook
is_singular( array( 'foo', 'bar', 'baz' ) )
// Returns true if the post_type is "foo", "bar", or "baz".
// See also the Custom Post Types book.
or if you aim at filtering you can always hook to pre_get_post with is_main_query() or any other conditional //

Resources