How to detect the current template being displayed in WordPress? - wordpress

I am developing a child theme of Twenty Eleven theme. There I need some customization in the blog page. I want to add a class in the place of id = "primary". How to discover which template is rendering the page?

Use the following code in your child theme's functions.php.
If you don't have it, create an empty one and use only the php opening tag (<?php).
This will print in the_content the current theme and template being displayed.
Seeing the main site page: child theme have an index.php
Seeing a single post: child theme don't have a single.php
Using a filter to add debugging to the content: check the comments
add_filter( 'the_content', 'so_13737534_print_template', 20 );
function so_13737534_print_template( $content )
{
// $template value equals to:
// /public_html/wp-content/themes/theme-name/template-name.php
global $template;
// Return normal content if seeing the backend
// or the user is not administrator
if( is_admin() || !current_user_can( 'delete_plugins' ) )
return $content;
// Split the value and build the output html
$split_path = explode( '/', $template );
$total = count( $split_path ) - 1;
$theme_and_template = $split_path[$total-1] . '/' . $split_path[$total];
$print = '<strong style="font-size:1em;background-color:#FFFDBC;padding:8px">Current = ' . $theme_and_template . '</strong>';
// Add our output before the_content
$content = $print . $content;
return $content;
}
The same can be printed as an Html comment in the <head>, using:
add_action( 'wp_head', 'so_13737534_print_template_in_head', 999 );
function so_13737534_print_template_in_head()
{
global $template;
echo '
<!--
TEMPLATE = ' . $template . '
-->
';
}

Related

How to echo something after header tag (wordpress)?

I want to echo something after header tag in single pages.
I have a filter to add something before content.
function add_custom_meta_to_content($content) {
$queried_object = get_queried_object();
if ( $queried_object ) {
$post_id = $queried_object->ID;
}
$text = get_post_meta($post_id, 'textbox_wporg_meta_key', true);
if( is_single() ) {
$content = $text . '' . $content;
}
return $content;
}
add_filter( 'the_content','add_custom_meta_to_content' );
But Now I want add some data exactly after header tag in single pages by my plugin.
I don't want to edit theme codes.
How can I do that?
You wouldn't normally do this with the the_content filter. Almost all commercial themes would have a specific hook to insert additional code after the post title and before the main content. For instance, the Generatepress framework would enable you to hook into the generate_after_entry_title action hook and you would output additional code there.

WordPress RSS list add featured image link

I want wordpress post rss list featured image link
Ex:
<title>Test title</title>
<desc>Test desc</desc>
<image>imagelink</image>
I'm not using plugin.
Thanks
This will do. Add it to functions.php file:
// add the namespace to the RSS opening element
add_action( 'rss2_ns', 'custom_prefix_add_media_namespace' );
function custom_prefix_add_media_namespace() {
echo "xmlns:media="http://search.yahoo.com/mrss/"n";
}
// add the requisite tag where a thumbnail exists
add_action( 'rss2_item', 'custom_prefix_add_media_thumbnail' );
function custom_prefix_add_media_thumbnail() {
global $post;
if( has_post_thumbnail( $post->ID )) {
$thumb_ID = get_post_thumbnail_id( $post->ID );
$details = wp_get_attachment_image_src($thumb_ID, 'thumbnail');
if( is_array($details) ) {
echo '<media:thumbnail url="' . $details[0] . '" width="' . $details[1] . '" height="' . $details[2] . '" />';
}
}
}
Also when you add this, be sure to reset the permalinks (flush them), by going to Settings > Permalinks, and either change permalinks to default, and back to your defined settings, or just re-saving.
After that, check your feed and the media should be there.

Wordpress Gallery Shortcode Pregreplace

Basically I need to remove the gallery shortcode from the Wordpress content, I'm using
echo preg_replace('/\[gallery ids=[^\]]+\]/', '', get_the_content() );
It is removing the gallery shortcode successfully, but also the paragraph tags which I need to keep. The idea is that I want to output everything in the content except the gallery.
You could use Wordpress strip_shortcode function.
Look at the example in the Codex.
You can create a filter that strips shortcodes:
function remove_shortcode_from($content) {
$content = strip_shortcodes( $content );
return $content;
}
and call it when you need (in your template):
add_filter('the_content', 'remove_shortcode_from');
the_content();
remove_filter('the_content', 'remove_shortcode_from')
EDIT 1
Another way to get that (and answering you comment) you can use Wordpress apply_filters function in the content after remove the undesirables shortcodes.
//within loop
$content = get_the_content();
$content = preg_replace('/\[gallery ids=[^\]]+\]/', '', $content );
$content = apply_filters('the_content', $content );
echo $content;
But I would not recommend to you to do that. I think forcing your site to modify the content of a post could make that hard to understanding. Maybe you should work with Wordpress Excerpt and avoid any problem.
A link that helped me
to remove a shortcode or a particular list of shortcode you can use this code.
global $remove_shortcode;
/**
* Strips and Removes shortcode if exists
* #global int $remove_shortcode
* #param type $shortcodes comma seprated string, array of shortcodes
* #return content || excerpt
*/
function dot1_strip_shortcode( $shortcodes ){
global $remove_shortcode;
if(empty($shortcodes)) return;
if(!is_array($shortcodes)){
$shortcodes = explode(',', $shortcodes);
}
foreach( $shortcodes as $shortcode ){
$shortcode = trim($shortcode);
if( shortcode_exists($shortcode) ){
remove_shortcode($shortcode);
}
$remove_shortcode[$shortcode] = 1;
}
add_filter( 'the_excerpt', 'strip_shortcode' );
add_filter( 'the_content', 'strip_shortcode' );
}
function strip_shortcode( $content) {
global $shortcode_tags, $remove_shortcode;
$stack = $shortcode_tags;
$shortcode_tags = $remove_shortcode;
$content = strip_shortcodes($content);
$shortcode_tags = $stack;
return $content;
}
dot1_strip_shortcode( 'gallery' );
Accepts single, comma seprated shortcode string or array of shortcodes.

Change the page title using a short code

I am coding a plugin and I use a short code to render a page content. I want to change the page title. I used this:
// Change the title of the page of the charts to add the current month.
add_filter('the_title', 'charts_title', 10, 2);
function charts_title($title, $id) {
$title .= ' ' . date('F Y');
return $title;
}
But it does that for all the posts and pages. Can I do that only for the pages that contains the short code I created ? I tried to do that, but it doesn't work.
add_shortcode('charts-vote', 'charts_vote');
function charts_vote($atts) {
// Add the filter only in the short code function callback.
add_filter('the_title', 'charts_title', 10, 2);
// ... //
return $content;
}
Can someone please help me ?
I understand that the specifics of your set up may require checking for the Shortcode, but maybe a Custom Field could be used for that:
add_filter( 'the_title', 'charts_title_so_15312385', 10, 2 );
function charts_title_so_15312385( $title, $post_id )
{
// Admin area, bail out
if( is_admin() )
return $title;
// Custom field not set, bail out
$mod_title = get_post_meta( $post_id, 'mod_title', true );
if( !$mod_title )
return $title;
// Ok, modify title
$title .= ' ' . date( 'F Y' );
return $title;
}
The Shortcode could even "talk" with the Custom Field for extended configurations.
For ease of use, you could make a Custom Meta Box or use a plugin like Advanced Custom Fields.

Is there any way to override a WordPress template with a plugin?

I'd like to make a landing page. If plugin detects some GET or POST requests it should override wordpress theme and show its own.
It would work somehow like that:
if (isset($_GET['action']) && $_GET['action'] == 'myPluginAction'){
/* do something to maintain action */
/* forbid template to display and show plugin's landing page*/
}
I'm familiar with WP Codex, but I don't remember if there is any function to do that. Of course, I googled it with no results.
Thanks for any ideas in advance.
You need the hook template_include. It doesn't seem documented in the Codex, but you can find more examples here in SO or in WordPress StackExchange
Plugin file
<?php
/**
* Plugin Name: Landing Page Custom Template
*/
add_filter( 'template_include', 'so_13997743_custom_template' );
function so_13997743_custom_template( $template )
{
if( isset( $_GET['mod']) && 'yes' == $_GET['mod'] )
$template = plugin_dir_path( __FILE__ ) . 'my-custom-page.php';
return $template;
}
Custom Template in Plugin folder
<?php
/**
* Custom Plugin Template
* File: my-custom-page.php
*
*/
echo get_bloginfo('name');
Result
Visiting any url of the site with ?mod=yes will render the plugin template file, e.g.: http://example.com/hello-world/?mod=yes.
you need to create a folder '/woocommerce/' inside your plugin directory, inside woocommerce you need to add a folder say, for email 'emails' and put the required template inside '/emails/' to override. just copy paste this code in the main.php of your plugin.
<?php
/**
* Plugin Name: Custom Plugin
*/
function myplugin_plugin_path() {
// gets the absolute path to this plugin directory
return untrailingslashit( plugin_dir_path( __FILE__ ) );
}
add_filter( 'woocommerce_locate_template', 'myplugin_woocommerce_locate_template', 10, 3 );
function myplugin_woocommerce_locate_template( $template, $template_name, $template_path ) {
global $woocommerce;
$_template = $template;
if ( ! $template_path ) $template_path = $woocommerce->template_url;
$plugin_path = myplugin_plugin_path() . '/woocommerce/';
// Look within passed path within the theme - this is priority
$template = locate_template(
array(
$template_path . $template_name, $template_name
)
);
// Modification: Get the template from this plugin, if it exists
if ( ! $template && file_exists( $plugin_path . $template_name ) )
$template = $plugin_path . $template_name;
// Use default template
if ( ! $template )
$template = $_template;
// Return what we found
return $template;
}
?>
for reference template override using plugin

Resources