Placing PHP code in social locker PHP shortcode - php

I am using wordpress. I am using the plugin social locker.
I have figured out how to display the shortcode via PHP:
<?php echo do_shortcode('[sociallocker id="3071"] My content Here [/sociallocker]'); ?>
However I want to display this button that will allow for a print popup in between there.
This code is:
<?php if ( function_exists( 'pdf_print_popup' ) ) pdf_print_popup(); ?>
So my question is can I do this:
<?php echo do_shortcode('[sociallocker id="3071"] <?php if ( function_exists( 'pdf_print_popup' ) ) pdf_print_popup(); ?> [/sociallocker]'); ?>

You have opening / closing PHP tags inside your echo statement.
Try this instead:
<?php
ob_start();
if ( function_exists( 'pdf_print_popup' ) ) {
pdf_print_popup();
}
$content = ob_get_clean();
echo do_shortcode( '[sociallocker id="3071"]' . $content . '[/sociallocker]' ); ?>
You'd be better modifying pdf_print_popup to return instead of output a value as well removing the need for output buffering.

Related

How to use complex curly braces in external template file

I am trying to interpolate a string from an external file but I am having issues using complex curly braces. The variables do not populate as expected? If i take the code from the external template file it successfully returns a value for each variable.
index.php
function getIncludeContents($included_file_path, $post){
if ( !file_exists( __DIR__ . $file ) ) {
return 'nofile';
}
if ( is_array( $post ) ){
extract( $post );
}
ob_start();
include($included_file_path);
return ob_get_clean();
}
$navigation = getIncludeContents('layouts/test.php', $post);
var_dump($navigation);
test.php
<div id="post-{$post['id']}"></div>
this returns...
'<div id="post-${post['id']}"></div>'
when it should return...
'<div id="post-201"></div>
any ideas why? thanks
Since test.php doesn't have <?php, it's being treated as fixed text, not PHP code to execute. It needs echo statements to echo variables, or you can use the short echo tag.
<div id="post-<?= $post['id'] ?>"></div>
Try this way
<div id="post-<?php echo $post['id']?>"></div>

Modifying single.php on child theme "Mikado Newsflash"

I have a website with "Mikado Newsflash" theme and a custom child theme. I can add functions on child theme and that, but if I modify single.php I get no results. Even if I delete the code on main theme it still works. Is it being called somewhere else? I've modified core templates such in framework/modules/blog/templates/singles and no result. Any clue on how to override single.php ?
This is the single.php theme file:
<?php
get_header();
newsflash_mikado_get_title();
get_template_part( 'slider' );
if ( have_posts() ) : while ( have_posts() ) : the_post();
//Get blog single type and load proper helper
newsflash_mikado_include_blog_helper_functions( 'singles', 'standard' );
//Action added for applying module specific filters that couldn't be applied on init
do_action( 'newsflash_mikado_blog_single_loaded' );
//Get classes for holder and holder inner
$mkdf_holder_params = newsflash_mikado_get_holder_params_blog();
?>
<div class="<?php echo esc_attr( $mkdf_holder_params['holder'] ); ?>">
<?php do_action( 'newsflash_mikado_after_container_open' ); ?>
<div class="<?php echo esc_attr( $mkdf_holder_params['inner'] ); ?>">
<?php newsflash_mikado_get_blog_single( 'standard' ); ?>
</div>
<?php do_action( 'newsflash_mikado_before_container_close' ); ?>
</div>
<?php endwhile; endif;
get_footer(); ?>
Most of the times, when an edit on a template file doesn't change anything is because you are changing the wrong file.
Add this code to your theme's functions.php file:
functions.php
function cagb_which_template_is_loaded() {
if ( is_super_admin() ) {
$categories = get_the_category();
//$category_id = $categories[0]->cat_ID;
echo '<div style="margin-top:25px;" id="my-debug">';
global $template;
print_r( $template );
echo ' | ' . get_the_ID();
echo ' | ' . $categories;
echo "</div>";
}
}
add_action( 'wp_footer' , 'cagb_which_template_is_loaded');
When logged in, this addon will add extra information at the bottom of the page (for debug), when you load the homepage (or any other page) it will show something like this:
/www/public_html/wp-content/themes/mikado-newsflash/homepage.php
This way you will know which file of your template is loaded, and you now can edit it.

Hide after X characters in php?

I have a Wordpress site, and I want to set up a paywall for certain content.
I would like to show some intro of each such article for Google index and users as well, but rest of it would be hidden for guests and available for logged in (in my case paid users).
I successfully implemented is_user_logged_in() PHP statement from Wordpress, but I am not finding PHP code anywhere online about hiding everything after n-characters, or words.
My intended workflow is:
<?php
if ( is_user_logged_in() ) {
the_content();
} else {
echo 'show only 200 characters or words of the content code should be here';
}
?>
You can use WordPress default function wp_trim_words.
<?php
if ( is_user_logged_in() ) {
the_content();
} else {
$content = get_the_content(); // $content is whatever your content field.
echo wp_trim_words( $content, 200, ' ' );
}
?>
<?php
if ( is_user_logged_in() ) {
the_content();
} else {
$content = get_the_content();
//If your template has some other way of getting the post content then use that variable.
echo substr($content, 0, strpos($content, ' ', 200));
}
?>

Print all en-queued scripts/styles

I am trying to add async attribute in my rander js and css script .
for these I amusing this script for print en-queued script .but this code is
only print some admin js not more.I am using this code from git hub and other stack overflow solution , but not able to print all script .
<?php
/*
* Getting script tags
* Thanks http://wordpress.stackexchange.com/questions/54064/how-do-i-get-the-handle-for-all-enqueued-scripts
*/
add_action( 'wp_print_scripts', 'wsds_detect_enqueued_scripts' );
function wsds_detect_enqueued_scripts() {
global $wp_scripts;
foreach( $wp_scripts->queue as $handle ) :
echo $handle . ' | ';
endforeach;
}
?>
If you wanna see all the registered or enqueued scripts then try the following code. $wp_scripts->queue will only show those scripts that are going to be printed or added to site.
add_action( 'wp_print_scripts', 'wsds_detect_enqueued_scripts' );
function wsds_detect_enqueued_scripts() {
global $wp_scripts;
foreach( $wp_scripts->registered as $script ) {
echo $script->handle . ' | ';
}
}

How to create a shortcode for Admin Theme Option - Wordpress

I am trying to create a shortcode for 2 of my theme's options. Basically I want to grabe my theme options to my shortcode so that I can display them anywhere on my posts something like this:
[ads_1]
[ads_2]
However when I tried to get the options on my shortcode it wont work at all and its giving me an error
Parse error: syntax error, unexpected 'theme_settings' (T_STRING) in C:\xampp\htdocs\themewp\wp-content\themes\bots_final\shortcodes.php on line 86
Here's my snippet for my shortcode where in I am trying to grab my theme option data:
add_shortcode('ads_1', function($atts){
return '
<div>
<?php $options = get_option( 'theme_settings' ); ?>
<?php echo $options['banner1']; ?>
</div>';
});
I am trying to grab the options from my Theme options page. Check my theme options code:
<div>
<?php $options = get_option( 'theme_settings' ); ?>
<?php echo $options['banner1']; ?>
</div>
Any idea what's the problem why I can't grab it?
You opened the <?php inside an open php tag:
add_shortcode('ads_1', function($atts){
$html = '<div>';
$options = get_option( 'theme_settings' );
$html .= $options['banner1'];
$html .= '</div>';
return $html;
});
What do you get from get_option( 'theme_settings' );? It seems like your suggesting an array..
To avoid conflicts, I'd store your settings as 'mytheme_theme_settings'.

Categories