Remove Wordpress body classes on search page - php

I'm seeing a lot of guides on how to add more classes to a Wordpress body tag, but is there a way to remove all classes from a specific page template, more specifically the search page?
Here's where I'm at so far.. not much sorry.
add_filter('body_class', 'remove_search_class');
function remove_search_class($classes){
global $post;
if(is_search()) {
// find ".search & .search-results"
}
return // no classes within <body>
}

add_filter('body_class','alter_search_classes');
function alter_search_classes($classes) {
if(is_search()){
return array();
} else {
return $classes;
}
}
try adding this in your functions.php

Related

Overwrite plugin function in child theme wordpress

Maybe this has been asked somewhere, but let me try to explain:
In a plugin of my wordpress website, I have a function that I want to overwite, but it won't let me.
Here is how it looks like:
function this_is_funtionA() {
$content = this_is_functionB('somevalue1', 'somevalue2'); // this function I am trying to overwrite
return $content;
}
add_filter('the_content', 'this_is_funtionA', 5);
function this_is_funcionB($item1, $item2) {
// some code
}
How can I overwrite this_is_functionB?

How to create shortcodes with function inside function in wordpress

In my theme function.php i am trying to add shortcodes for myHeader, myFooter etc .
Inside myHeader(), myFooter() function i added another function like fn_1(), fn_2(), fn3_() etc these function would be change weekly or monthly basis.
Is it possible to call a shortcode as written below
function myHeader(){
fn_1();
//fn_2();
}
function myFooter(){
fn_2();
// fn_3();
}
add_shortcode('myFooter', 'myHeader');
add_shortcode('myFooter', 'myFooter');
function fn_1(){
return 'something for 1';
}
function fn_2(){
return 'something for 2';
}
function fn_3(){
return 'something for 3';
}
In my post i call my shortcode as [myHeader] and [myFooter]
It is possible, you just need to return something inside your shortcode methods. Shortcode functions also require some variables, though they don't actually have to be used.
eg.
function myHeader($atts, $content = null){
$temp = fn_1();
return $temp;
}
$atts is required to create shortcode if you are passing $atts or not.

Any way to hook function into post footer in WordPress (not wp_footer)?

You can hook functions into the site footer using the following:
function to_footer() {
$content = 'I am in the footer';
echo $content;
}
add_action('wp_footer', 'to_footer');
But is there a similar approach to add a function inside the post's footer (not site footer) in single page views?
The closest you can get (without changing template files) is this
function to_footer($content)
{
return $content . 'I am in the footer';
}
add_action('the_content', 'to_footer');
This will add your thing after post content
If you do not mind editing your templates, try the following
function alt_footer()
{
do_action('alt_footer');
}
in functions.php of your theme. Then call alt_footer() in your template where you need it, then
function to_footer()
{
echo 'I am in the footer';
}
add_action('alt_footer', 'to_footer');

plugin_action_links not working in WordPress 2.8+

I developed a plugin with Settings link that was working fine in WordPress 2.7. Version 2.8 brings some additional security features that cause Settings link displaying message: You do not have sufficient permissions to access this page.
This is the API hook I use to create link:
function folksr_plugin_action($links, $file) {
if (strstr($file, 'folksr/folksr.php')) {
$fl = "Settings";
return array_merge(array($fl), $links);
}
return $links;
}
add_filter('plugin_action_links', 'folksr_plugin_action', 10, 2);
Full source code available at plugin page.
Settings screen does not contain any additional logic, just a couple of options and HTML echoed to the screen.
Suprisingly enough, Codex does not return anything for search phrase "plugin_action_links". Can you provide example or point me to working code for Settings link in Plugins menu?
I found the solution to my own problem by analyzing sources of some random plugins. I must say - what an unpleasurable experience that was! But hey, here's the solution.
It turns out that in order to build Settings link, it needs to be registered first. The following code is a stub that does the trick:
class MyPlugin {
public function __construct() {
add_filter('plugin_action_links', array($this, 'renderPluginMenu'), 10, 2);
add_action('admin_menu', array($this, 'setupConfigScreen'));
}
public function renderPluginMenu() {
$thisFile = basename(__FILE__);
if (basename($file) == $thisFile) {
$l = 'Settings';
array_unshift($links, $l);
}
return $links;
}
public function setupConfigScreen() {
if (function_exists('add_options_page')) {
add_options_page('MyPlugin settings', 'MyPlugin', 8, basename(__FILE__), array($this, 'renderConfigScreen'));
}
}
public function renderConfigScreen() {
include dirname(__FILE__) . '/MyPluginSettings.php';
}
}
I have working admin menu links on my plugins working in 2.8+. My function looks like this:
function plugin_action_links( $links, $file ) {
if ( $file == plugin_basename(__FILE__) )
$links[] = 'Settings';
return $links;
}
My add_filter line is mostly identical. I think the first thing to try is adding the use of the admin_url function.
Hope that helps.

Output content from wordpress plugin and rewrite rules

I have created a wordpress plugin that has a filter on the_content, looking for a specific tag then outputting the plugin content in place of that tag.
I would now like to use rewrite rules to call the plugin and output the data within the template, but I'm not finding much help.
Can someone provide an example, or some guidance on how to add a rewrite rule using the built in wp methods and calling my methods in the plugin which outputs some content.
Ideally, I would like shop/ to be matched and then pass everything after shop to my dispatch method on my plugin so that I can have shop/category/shirts or shop/product/the-cool-shirt. My dispatch method would handle breaking apart the rest of the url and calling methods accordingly.
This can get rather interesting. I had to do this for a plugin at one point, I don't have it in front of me, so this it out of memory, but the general idea should be correct.
<?php
add_action('init', 'rewrite_rules');
function rewrite_rules() {
global $wp, $wp_rewrite;
$wp_rewrite->add_rule('(widget1|widget2|widget3)/([a-zA-Z0-9_-]{3,50})$', 'index.php?pagename=listing&category=$matches[1]&subcategory=$matches[2]', 'top' );
$wp->add_query_var( 'category' );
$wp->add_query_var( 'subcategory' );
$wp_rewrite->flush_rules();
}
?>
Using regular expressions is a monumental task in itself, I believe I used this site: http://tools.netshiftmedia.com/regexlibrary/ for help.
I also used FakePage plugin to actually display my custom "dynamic" pages, as I called them, but I suppose everything in WP is technically dynamic.
http://scott.sherrillmix.com/blog/blogger/creating-a-better-fake-post-with-a-wordpress-plugin/
Let me know if you need more help.
I did something very similar not long ago, and I did it by cheating.
If you find the built in rewrite rules too complicated or unable to do the job, you may find it easier to catch the request and filter the results. A simplified version:
add_action('parse_request', 'my_parse_request');
function my_parse_request (&$wp) {
$path = $wp->request;
$groups = array();
if (preg_match("%shop/product/([a-zA-Z0-9-]+)%", $path, $groups)) {
$code = $groups[1];
$product = get_product($code); // your own code here
if (isset($product)) {
add_filter('the_posts', 'my_product_filter_posts');
}
}
}
function my_product_filter_posts ($posts) {
ob_start();
echo "stuff goes here"; // your body here
$content = ob_get_contents();
ob_end_clean();
return array(new DummyResult(0, "Product name", $content));
}
To explain:
The action on parse_request is called before the database lookup. Based on the URL, it installs the other actions and filters.
The filter on posts replaces the results of the database lookup with fake results.
DummyResult is a simple class that has the same fields as a post, or just enough of them to get away with it:
class DummyResult {
public $ID;
public $post_title;
public $post_content;
public $post_author;
public $comment_status = "closed";
public $post_status = "publish";
public $ping_status = "closed";
public $post_type = "page";
public $post_date = "";
function __construct ($ID, $title, $content) {
$this->ID = $ID;
$this->post_title = $title;
$this->post_content = $content;
$this->post_author = get_default_author(); // implement this function
}
}
There's a lot of homework left for the reader in the above, but it's an ugly, working approach. You'll probably want to add a filter for template_redirect, to replace the normal page template with a product-specific one. And you may need to adjust the URL regex if you want pretty permalinks.

Categories