Wordpress custom plugin will display on post but will not print - wordpress

Here is a simplified version of the plugin. It does what it is supposed to do. It displays the modified time like Modified: February 23, 2016.
<?php
/**
*/
function modified_date($content) {
$post = get_post();
if (is_page()) {
}
else {
$origdate = get_the_time();
$moddate = get_the_modified_time();
if ($origdate != $moddate) {
echo '<div class="mod" style="line-height: 1.2em; position: relative; text-align: right;"><strong>Modified:</strong> ';
echo the_modified_date();
echo '</div>';
}
}
return $content;
}
add_filter('the_content', 'modified_date');
?>
I have tried add_action and add_shortcode with adjustments. Both displayed the modified date on the post, but not in print.
I have tried many plugins to print my post, but none pick up the modified time. The latest plugin, I have used Print My Blog displays everything I want to see in a print except the modified time. The easiest Print My Blog configuration that works (again except for the modified time) is:
Print My Blog > Settings > Print Buttons > select Posts
Then Customize Buttons > select Print
Is there something that I am missing? Any help would greatly be appreciated.

<?php
/**
*/
function modified_date($content) {
if (is_singular('page')) {
return $content;
}
else {
$origdate = get_the_time();
$moddate = get_the_modified_time();
if ($origdate != $moddate) {
$content = '<div class="mod" style="line-height: 1.2em; position: relative; text-align: right;"><strong>Modified:</strong> '. get_the_modified_date() .'</div>'.$content;
return $content;
}
}
}
add_filter('the_content', 'modified_date');
?>

Beware that in your function if $origdate == $moddate you are not returning anything and the page will be empty. It's better to return $content at the end of the function to make sure it returns a value.
<?php
function modified_date($content) {
if( ! is_singular('page')) {
$origdate = get_the_time();
$moddate = get_the_modified_time();
if ($origdate != $moddate) {
$content = '<div class="mod" style="line-height: 1.2em; position: relative; text-align: right;"><strong>Modified:</strong> '. get_the_modified_date() .'</div>'.$content;
}
}
return $content;
}
add_filter('the_content', 'modified_date');
?>

Related

ACF block background colour in admin panel

I have a bunch of ACF blocks (within an accordion), have added a status field for each block (active or disabled). Basically I want to be able to hide a block on front-end without needing to delete it in admin side, I can do that bit.
Struggling with back-end, if a block status is set to 'disabled', I would like for it be obvious in back-end, so want a different background to other blocks. Below is the code so far. It does not seem understand the if statement, it just adds red bg on all blocks regardless of the status. If I echo out the status, that seems to be assigned to the right block, but if statment doesn't seem to recognise that for some reason.
Just to be clear I need to target blocks which are in the admin panel when editing a page or creating. I have multiple ACF blocks each inside ACF accordion for making it a bit tidy.
Any help would be much appreciated. Thanks.
function my_acf_admin_head() {
global $post;
$post_blocks = parse_blocks( get_the_content( '', false, $post->id ) );
foreach ($post_blocks as $block) {
if ( isset( $block['attrs']['data']['status'] ) ) {
if($block['attrs']['data']['status'] == 'disabled') {
?>
<style>.acf-accordion { background-color: red } </style>
<?php
} else {
?><style>.acf-accordion { background-color: orange } </style><?php
}
}
}
}
add_action('acf/input/admin_head', 'my_acf_admin_head');
function status_acf_blocks( $block ) {
$bg_color = false;
if ( is_admin() ) {
if ( $block == 'disabled' ) {
$bg_color = '<style>.acf-block-wrapper { background-color: red; }</style>';
} else {
$bg_color = '<style>.acf-block-wrapper { background-color: orange; }</style>';
}
}
return $bg_color;
}
Acf block file:
...
<?php
$status = get_field( 'slug' );
echo status_acf_blocks( $status );
?>
<div class="acf-block-wrapper">
</div>
...

Why is the filter the_title not working when the_content is working fine?

Function and filter to display a player post titles:
function wpb_after_post_content($content){
if (is_single()) {
$id=get_the_ID();
$post_meta=get_post_meta(get_the_ID(), 'enclosure', TRUE);
$mp3=explode('.mp3',$post_meta);
$title=get_the_title();
if(count($mp3)>1)
{
$player = '<div class="post_player">' . do_shortcode('[fap_track url="'.trim($mp3[0]).'.mp3" title=url="'.$title.'" share="" cover="" meta="" layout="list" enqueue=no auto_enqueue=yes]')."</div>";
$content=$player.$content;
}
}
if (is_single()) {
$id=get_the_ID();
$post_meta=get_post_meta(get_the_ID(), 'enclosure', TRUE);
$mp3=explode('.mp3',$post_meta);
$title=get_the_title();
if(count($mp3)>1)
{
$player = '<div class="zoomsounds_player">'. do_shortcode('[zoomsounds_player source="'.trim($mp3[0]).'.mp3" play_in_footer_player="on" config="default" artistname="'.get_post_meta($id, 'arxiu_de_so', true).'" config="" ]');
/**if (wp_is_mobile())*/ {
if(get_post_meta($id, 'arxiu_de_so', true)!=""){
/** $player .= " <p id='arxiu_so'> Arxiu de so: ".get_post_meta($id, 'arxiu_de_so', true)."</p>";*/
/**$player .= '<span id="arxiuVal" style="opacity:1"> '.get_post_meta($id, 'arxiu_de_so', true).'</span>';*/
}
}
$player .="</div>";
$content=$player.$content;
}
}
return $content;
}
add_filter( "the_content", "wpb_after_post_content" );
The above code works fine with the_content filter and displays the player below the featured image. However, when I use this function with the the_title filter, the page doesn't load at all. The reason I want to use the_title is because I want the player to display above the featured image but below the post title.
Screenshot:-
This screenshot is to show that with the_content the player displays however it displays below post image.
Please have a look as below:
If you want to use the_title(), then you no need to pass the post_id in the the_title(), we have some params for the_title() if you want to add them like as below:
$before
(string) (Optional) Markup to prepend to the title.
Default value: ''
$after
(string) (Optional) Markup to append to the title.
Default value: ''
$echo
(bool) (Optional) Whether to echo or return the title. Default true for the echo.
Default value: true
If you want to use get_the_title(), then you must need to pass post_id inside the get_the_title($postId);
According to your code snippet, you must update your code with as below for get_the_title():
<?php
function wpb_after_post_content($content){
if (is_single()) {
$id=get_the_ID();
$post_meta=get_post_meta(get_the_ID(), 'enclosure', TRUE);
$mp3=explode('.mp3',$post_meta);
$title=get_the_title($id);
if(count($mp3)>1) {
$player = '<div class="post_player">' . do_shortcode('[fap_track url="'.trim($mp3[0]).'.mp3" title=url="'.$title.'" share="" cover="" meta="" layout="list" enqueue=no auto_enqueue=yes]')."</div>";
$content=$player.$content;
}
}
return $content;
}
add_filter( "the_content", "wpb_after_post_content" );
?>
And for the the_title(), you should update your code with as below:
<?php
function wpb_after_post_content($content){
if (is_single()) {
$id=get_the_ID();
$post_meta=get_post_meta(get_the_ID(), 'enclosure', TRUE);
$mp3=explode('.mp3',$post_meta);
$title=get_the_title();
if(count($mp3)>1)
{
$player = '<div class="post_player">' . do_shortcode('[fap_track url="'.trim($mp3[0]).'.mp3" title=url="'.the_title().'" share="" cover="" meta="" layout="list" enqueue=no auto_enqueue=yes]')."</div>";
$content=$player.$content;
}
}
return $content;
}
add_filter( "the_content", "wpb_after_post_content" );
?>
NOTE: You have not declared the title parameter in the zoomsounds_player shortcode so please pass and update accordingly.
I hope it would help you out.

How to display blog category as title in Voltage Wordpress theme?

My client has a site that was built using Themeforest's Voltage Wordpress theme. I have all
of the tweaking they want done except for one thing.
I have created 3 blog categories, with each one being a nav bar item. When I click any of them,
the page that comes has the correct posts on it, but the header on all three pages reads
"Blog Archive".
See here: http://s11.mynewsitereview.com/
The three categories are Fashion & Culture, My Style, and Hints & Tips.
Here is the code from index.php that generates the page title:
<h1><?php
if(single_cat_title("", false)=='' && !is_archive()) {
$page = get_page_by_title( single_post_title('', false) );
$vltg_page_meta=get_post_meta($page->ID, '_vltg_page_meta', true);
if(isset($vltg_page_meta['display_title']) && $vltg_page_meta['display_title']!='') {
echo $vltg_page_meta['display_title'];
} else {
single_post_title();
}
} else if(is_archive()) {
if ( have_posts() ) {
the_post();
if(is_day()) {
echo get_the_date();
} else if(is_month()) {
echo get_the_date('F Y');
} else if(is_year()) {
echo get_the_date('Y');
} else {
echo 'Blog Archives';
}
} else {
echo 'Blog Archives';
}
rewind_posts();
} else {
single_cat_title();
} ?></h1>
I can see why it is generating "Blog Archives" as the title, but I don't know how to tweak it
so that it will display the category name instead.
Any help would be most appreciated!
:)
OK, I finally solved this one after getting no love from the developer of the Voltage theme.
Have a look at this part of the code I posted in my original question:
} else {
echo 'Blog Archives';
}
} else {
echo 'Blog Archives';
}
Here is what I changed it to:
} else {
single_cat_title();
}
} else {
single_cat_title();
}
And it works!

Change div background acording to the selected category

I'm coding a graphic menu that consist in five divs with a background each. On mouseover the background changes and the child div disappears. On click, each one works like a link to the correspondent category. I want to apply some changes to the clicked div, so I was wonder if there is some function in wordpress to get the selected category. So I can check for It and in function of wich category is selected apply the changes to the correspondent div.
Depending on your theme, you might be able to extract this information from the body tag or post wrapper div, which gets dynamically populated with a ton of juicy information in the form of classes.
If your theme doesn't have this, you could try The Mother Of All WordPress Body Tags in your theme:
<body
id="
<?php
$post_parent = get_post($post->post_parent);
$parentSlug = $post_parent->post_name;
if (is_category()) { echo "CategoryPage"; }
elseif (is_search()) { echo "SearchResults"; }
elseif (is_tag()) { echo "Tag"; }
else { echo $parentSlug; }
?>"
class="
<?php
if (is_category()) { echo 'category'; }
elseif (is_search()) { echo 'search'; }
elseif (is_tag()) { echo "tag"; }
elseif (is_home()) { echo "home"; }
elseif (is_404()) { echo "page404"; }
else { echo $post->post_name; }
?>">
Reference: http://www.mimoymima.com/2010/03/lab/wordpress-body-tag/

wordpress shortcode problem

Apparently the following simple code breaks the shortcode API of wordpress. When I add this code in the function.php the shortcode API won't work.
This code is normally to add a text at the bottom of each page, any idea why?
function cmstut_basic_promote($content)
{
echo $content;
if(is_single())
{
?>
<div class="promote">
<h2>Enjoy this article?</h2>
<p>If you have enjoyed this article please subscribe to our RSS Feed</p>
</div>
<?php
}
}
add_filter('the_content', 'cmstut_basic_promote');
you must return the content from your filter not echo it - so something like
function cmstut_basic_promote($content) {
if(is_single()) {
return $content . '<div class="promote"><h2>Enjoy this article?</h2> ...';
} else {
return $content;
}
}
would be the way to go

Resources