I have download some plug-in to render youtube search inside my site, the shortcode working perfect at pages/posts but when I use <?php echo do_shortcode('[soundcloud_wpress]');?> over my index.php, it's doesn't show anything.
index.php:
<?php
/**
* The main template file
*
* This is the most generic template file in a WordPress theme
* and one of the two required files for a theme (the other being style.css).
* It is used to display a page when nothing more specific matches a query.
* e.g., it puts together the home page when no home.php file exists.
*
* Learn more: {#link https://codex.wordpress.org/Template_Hierarchy}
*
* #package WordPress
* #subpackage Twenty_Fifteen
* #since Twenty Fifteen 1.0
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<div class="row searchdiv-wrapper">
<div class="col-sm-6 youtube-search">
<?php echo do_shortcode('[youtube_wpress]');?>
</div>
<div class="col-sm-6 soundcloud-search">
<?php echo do_shortcode('[soundcloud_wpress]');?>
</div>
</div>
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php get_footer(); ?>
The plugin add shortcode:
$GLOBALS['ygp_youtube_wpress'] = get_option('ygp_youtube_wpress');
$GLOBALS['ygp_youtube_wpress']['plugin_code'] = 'youtube_wpress';
$GLOBALS['ygp_youtube_wpress']['item_name'] = 'youtube_wpress';
//error_reporting(E_WARNING);
require_once dirname( __FILE__ ).'/include/vbox/include/webzone.php';
require_once dirname( __FILE__ ).'/activation.php';
$a1=new Youtube_wpress_activation();
if($a1->verify_activation()) {
require_once dirname( __FILE__ ).'/youtube_widget.php';
}
if(is_admin()) {
require_once dirname( __FILE__ ).'/admin/options.php';
}
class Youtube_wpress {
function Youtube_wpress() {
if(is_admin()) {
register_activation_hook(__FILE__, array(__CLASS__, 'on_plugin_activation'));
//Settings link
add_filter( 'plugin_action_links', array(__CLASS__, 'plugin_action_links'), 10, 2);
}
//Shortcodes
add_shortcode('youtube_wpress', array(__CLASS__, 'youtube_wpress_shortcode'));
}
function add_scripts_wp_footer() {
}
I have added only the first top code from the plug-in class .
Any idea why echo shortcode not working only inside index.php ?
Thanks and sorry for beginner questions.
Here are some notes on how you should use the do_shortcode() function:
<?php
add_filter( 'the_content', 'do_shortcode', 11 ); // From shortcodes.php
// Use shortcode in a PHP file (outside the post editor).
echo do_shortcode( '' );
// In case there is opening and closing shortcode.
echo do_shortcode( '[iscorrect]' . $text_to_be_wrapped_in_shortcode . '[/iscorrect]' );
// Enable the use of shortcodes in text widgets.
add_filter( 'widget_text', 'do_shortcode' );
// Use shortcodes in form like Landing Page Template.
echo do_shortcode( '[contact-form-7 id="91" title="quote"]' );
// Store the short code in a variable.
$var = do_shortcode( '' );
echo $var;
?>
There is an exception to the built-in embed shortcode available with WordPress. In order to use this shortcode with do_shortcode(), you can do the following:
<?php
$embedurl = 'http://yourembeddableurl.com';
if ( ! empty( $embedurl ) ) {
$var = apply_filters( 'the_content', "" );
echo $var;
}
?>
Related
I am loading custom template via template_include to load single page of a type. But it redirects to home page if a static page is selected as homepage from Settings > Reading
But it works if Your latest posts is selected.
Here is my code
add_filter( "template_include", "change_template" );
function change_template ($template) {
if ( !is_page("game") && preg_match('/\/game\//',$_SERVER["REQUEST_URI"] ) ) {
remove_filter( 'template_redirect' );
$template = PLUGIN_ROOT . "single-game.php";
if ( file_exists( $template ) ) {
return $template;
exit;
}
}
return $template;
}
and single-game.php includes this
<?php get_header();?>
<div id='game-root'> </div>
<?php get_footer(); ?>
I work in multisite
I have a child theme of my parent theme to customize a page template.
In this customize page template I would like to display a shortcode, which allows to retrieve the url address of the site in question.
My function in my child theme file, function.php:
add_action( 'init', function() {
add_shortcode( 'site_url', function( $atts = null, $content = null ) {
return site_url();
} );
} );
I would like to declare this shortcode in my template customizer, like this:
/*
Template name: Test
*/
get_header(); ?>
<div>
<h1>Notre site web : [site_url]</h1>
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
get_template_part( 'content', 'page' );
endwhile;
endif;
?>
</div>
<?php get_footer(); ?>
````but the shotcode doesn't generate anything at all in my customize template.
I must have missed something... :-(
Can you help me ?
Thanks
This is because shortcodes that are hardcoded into templates don't get rendered by default. What you want to do is put the shortcode in the function do_shortcode() and then it should output correctly.
echo do_shortcode('[site_url]');
First time building a Wordpress plugin. I create a page and I want to assign it a page template from my plugin folder. My code shows the Template name in the selectable list in the page editor, but it is not attached to the page. Nor manually attaching does anything to the page. But the path for the file is correct.
My code to create a page:
$page_path = "this-is-a-campaign-landing-page";
$page_title = 'This is a Campaigns Page Title';
$page_content = 'THIS IS A CAMPAIGNS PAGE BODY';
$page_check = get_page_by_title( $page_path );
$page = array(
'post_type' => 'page',
'post_title' => $page_title,
'post_content' => $page_content,
'post_status' => 'publish',
'post_author' => $author->ID,
'post_slug' => $page_path
);
if (!isset($page_check->ID) && !get_page_by_path($page_path)) {
$page_id = wp_insert_post($page);
if ($page_id) {
$template = '/home/vagrant/src/wptest/wp-content/plugins/pm/campaign_page.php';
update_post_meta($page_id, '_wp_page_template', $template );
}
}
Separately I add the template with add_filters
function add_campaign_template ($templates) {
$templates['campaign_page.php'] = 'Campaign Page';
return $templates;
}
add_filter ('theme_page_templates', 'add_campaign_template');
function set_campaign_template ($template) {
if ('campaign_page.php' == basename ($template)) {
$template = '/home/vagrant/src/wptest/wp-content/plugins/pm/campaign_page.php';
}
return $template;
}
add_filter ('page_template', 'set_campaign_template');
The page creates with no errors. When I view or edit the page, Default template is selected and my template appears in the list. Selecting it manually has no effect. What did I miss?
My simple page template:
<?php
/**
* Template Name: Campaign Page
*
* #package PM
*/
// get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main">
<section class="outer-categories">
<div class="container-fluid">
<div class="row text-justify">
THIS IS THE RIGHT PAGE TEMPLATE
THIS IS THE RIGHT PAGE TEMPLATE
THIS IS THE RIGHT PAGE TEMPLATE
THIS IS THE RIGHT PAGE TEMPLATE
<div class="col-lg-12">
<?php
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/content', 'page' );
endwhile; // End of the loop.
?>
</div>
</div>
</div>
</section>
</main><!-- #main -->
</div><!-- #primary -->
<?php
// get_footer();
Why is it not adding the template? With debug on I get no error in debug.log
Wow, long day.
Change this:
function set_campaign_template ($template) {
if ('campaign_page.php' == basename ($template)) {
$template = '/home/vagrant/src/wptest/wp-content/plugins/pm/campaign_page.php';
}
return $template;
}
add_filter ('page_template', 'set_campaign_template');
to this:
function set_campaign_template ($template) {
if ('campaign_page.php' == basename ($template)) {
$template = 'campaign_page.php'; <---- FIX
}
return $template;
}
add_filter ('page_template', 'set_campaign_template');
I am using KLEO theme for my work.
I am trying to filter posts by category when the date archive is called.
I did this by modifying the core files of the theme. However when the code is run it goes into infinite loop and definitely doesn't produce the results required.
e.g. if I go to localhost/wordpress/2014/06?category=events it should show posts on that date that are only from this category. However it goes into the infinite loop.
The modified code is here:
<?php
/**
* The template for displaying Archive pages
*
* Used to display archive-type pages if nothing more specific matches a query.
* For example, puts together date-based pages if no date.php file exists.
*
* If you'd like to further customize these archive views, you may create a
* new template file for each specific one. For example, Twenty Fourteen
* already has tag.php for Tag archives, category.php for Category archives,
* and author.php for Author archives.
*
* #link http://codex.wordpress.org/Template_Hierarchy
*
* #package WordPress
* #subpackage Kleo
* #since Kleo 1.0
*/
get_header(); ?>
<?php
//Specific class for post listing */
$blog_type = sq_option('blog_type','masonry');
$template_classes = $blog_type . '-listing';
if ($blog_type == 'standard' && sq_option('blog_meta_status', 1) == 1) { $template_classes .= ' with-meta'; }
add_filter('kleo_main_template_classes', create_function('$cls','$cls .=" posts-listing '.$template_classes.'"; return $cls;'));
?>
<?php get_template_part('page-parts/general-title-section'); ?>
<?php get_template_part('page-parts/general-before-wrap'); ?>
<?php if ( have_posts() ) : ?>
<?php
if ($blog_type == 'masonry') {
echo '<div class="row">'
.'<div class="grid-posts kleo-isotope masonry">';
}
?>
<?php
function the_loop($the_query){
// Start the Loop.
while ( $the_query ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
if ($blog_type == 'masonry') {
get_template_part( 'page-parts/post-content-masonry');
}
else {
get_template_part( 'content', get_post_format() );
}
endwhile;
}
if( is_date() ){
if(isset($_GET["category"])){
$category_slug = trim($_GET["category"]);
$args=array('category_name = '.$category_slug.'');
$the_query = new WP_Query( $args );
$lol =$the_query->have_posts();
the_loop($lol);
}
}
else{
$the_query=have_posts();
the_loop($the_query);
}
?>
<?php
if ($blog_type == 'masonry') {
echo '</div>'
.'</div>';
}
?>
<?php
// page navigation.
kleo_pagination();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
<?php get_template_part('page-parts/general-after-wrap'); ?>
<?php get_footer(); ?>
Please help!!!
What action or filter can I use in a Wordpress plugin to dynamically replace the contents (i.e. not the header or footer) of a 404 error page?
Basically I'm looking for a 404 error page equivalent for the_content filter, which will filter the contents of an existing page.
Thank you for your time.
Note: I know I can manually modify the 404 error page for the current theme, but that is not the effect I am trying to achieve.
From this WordPress Answer: How to control output of custom post type without modifying theme?
Plugin file:
<?php
/*
Plugin Name: Plugin 404 Page
Plugin URI: http://stackoverflow.com/questions/14539884
Description: Use the plugin's template file to render a custom 404.php
Author: brasofilo
Author URI: https://wordpress.stackexchange.com/users/12615/brasofilo
Version: 2013.26.01
License: GPLv2
*/
class Universal_Template
{
public function __construct()
{
$this->url = plugins_url( '', __FILE__ );
$this->path = plugin_dir_path( __FILE__ );
add_action( 'init', array( $this, 'init' ) );
}
public function init()
{
add_filter( 'template_include', array( $this, 'template_404' ) );
}
public function template_404( $template )
{
if ( is_404() )
$template = $this->path . '/404.php';
return $template;
}
}
$so_14539884 = new Universal_Template();
And in the plugin folder, a file named 404.php:
<?php
/**
* The template for displaying 404 pages (Not Found).
*
* #package WordPress
* #subpackage Twenty_Twelve
* #since Twenty Twelve 1.0
*/
get_header(); ?>
<div id="primary" class="site-content">
MY 404!
</div><!-- #primary -->
<?php get_footer(); ?>
The solution depends on the content of 404.php file. If this file contains static text, like
_e( 'It seems we can’t find what you’re looking for...', 'twentyeleven' );
you can add your own filter
apply_filters( 'my_404_content', 'Default 404 message' );
and in functions.php (or in plugin)
add_filter( 'my_404_content', 'replace_404_message' );
function replace_404_message($message) {
return 'Error 404 - '.$message;
}
If 404.php uses built-in WP functions to display page content, you should check what filters they are supported.
You might be able to add a the_content filter with a conditional is_404 section:
function content_404($content) {
if (is_404()) {
// do some stuff with $content
}
// no matter what,
return $content;
}
add_filter( 'the_content', 'content_404' );
Note that this does assume that the 404.php page template has a the_content template tag in place.