Insert custom field value before content - php

I have a custom meta field that I would like to insert in the_content automatically so that my AMP plugin can render the custom field value in the same way as the_content.
Currently I am using this code to display it:
<?php $video_value = get_post_meta( get_the_ID(), '_video', true ); ?>
<?php if ( ! empty( $video_value ) ) {?>
<div class="video-container"><?php echo $video_value; ?></div>
<?php } else { ?>
<?php the_post_thumbnail(); ?>
<?php } ?>
But I would like the $video_value to be inserted automatically before the_content.

You can use the the_content filter to do this. You can read more about it on the WordPress developer site.
But the code could be looking something like this:
function my_custom_content_filter($content){
global $post;
$video_value = get_post_meta($post->ID, '_video', true);
if($video_value){
return '<div class="video-container">' . $video_value . '</div>' . $content;
}else{
return get_the_post_thumbnail($post->ID) . $content;
}
}
add_filter('the_content', 'my_custom_content_filter');
And you can add this code in you functions.php file.
Note This filter only works on the_content() and not get_the_content()

You can do something like this -
and add the conditions that you want -
You might need to access Global $post to get the meta value
function custom_weird_name_before_after($content) {
if(is_page() || is_single() || $yourOwnConditions == true) {
$beforecontent = 'This line will go before the content - populate with whatever.';
$aftercontent = 'This will come after the content - makes sense right?';
$fullcontent = $beforecontent . $content . $aftercontent;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter('the_content', 'custom_weird_name_before_after');
You can add this in functions.php

Related

WordPress Append a file to the_content

I'd like to include a file to the end of the main content (the_content) but using my code below it is being added before the content. Any ideas?
Thanks in advance
function add_listing_dashboard_content( $content ) {
global $page_id_dashboard;
$page_id_dashboard = get_field('dashboard_page', 'option');
if (is_page($page_id_dashboard) && is_user_logged_in() ) :
$content .include(plugin_dir_path(__FILE__) . '../templates/partials/dashboard-dashboard.php');
endif;
return $content;
}
add_action('the_content', 'add_listing_dashboard_content');
This will be the correct way of doing it:
function add_listing_dashboard_content( $content ) {
global $post;
ob_start();
include(plugin_dir_path(__FILE__) . '../templates/partials/dashboard-dashboard.php');
$append = ob_get_clean();
$page_id_dashboard = get_field('dashboard_page', 'option');
if (is_page($page_id_dashboard) && is_user_logged_in() ) :
$content.$append;
endif;
return $content;
}
add_action('the_content', 'add_listing_dashboard_content');
Check if it works.
NOTE: I do not know if your get_field is retrieving correctly a page_id, instead for testing purposes remove the if and check if field is being included, then work on the conditions.

IF statement php displaying content if another is empty

I have some fields (Wordpress) and sometimes one of them is empty. I want to display the content of 'the_excerpt' when the 'short_description' is not filled in.
here is what I came with:
if (empty(the_field('short_description'))) {
the_excerpt();
} else {
the_field('short_description');
}
Unfortunately it displays both short_description and the except after that. What is wrong here? Do I miss something? For me the code looks good.
To Check if value exists first use get_field() function instead of the_field()
Please have a look on example which shows how to check if a value exists before displaying it.
<?php if( get_field('short_description') ): ?>
<?php the_field('short_description'); ?>
<?php else: ?>
<?php the_excerpt(); ?>
<?php endif; ?>
Or you can user in another way like :
$isValue = get_field( "short_description" );
if( $isValue ) {
echo $isValue ;
} else {
the_excerpt();
}
WooCommerce: Show Custom Short Description When Empty
add_action( 'woocommerce_single_product_summary', 'bbloomer_echo_short_desc_if_empty',
21 );
function bbloomer_echo_short_desc_if_empty() {
global $post;
if ( empty ( $post->post_excerpt ) ) {
$post_excerpt = '<p class="default-short-desc">';
$post_excerpt .= 'This is the default, global, short description.<br>It will
show if <b>no short description has been entered!</b>';
$post_excerpt .= '</p>';
echo $post_excerpt;
}
}
This is a Right

WordPress run php code into the_content loop

I'm learning PHP and WordPress development, so I assumed that maybe here I'll find the answer or a tip.
I've restriced the_content based on user role. After the end of the_content I'd like to display button whitch is unique to the specific post. So here is the code which display that:
function displaycontent($content) {
if(is_singular( 'custom_post_type' )) {
$aftercontent = 'I Want To Add Code Here';
$fullcontent = $content . $aftercontent;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter('the_content', 'displaycontent');
And I'd like to insert code below into the underlined place above:
<?php
$post = $wp_query->post;
$group_id = get_field( 'link_number' , $post->ID );
if( $group_id ) {
echo do_shortcode( '[checkout_button class="button" level="' . $group_id . '" text="Order"]' );
}
?>
How can I do that?
It's probably better to create a custom shortcode for this. If you change how the_content works, it will be global, everywhere.
Note:
This code is completely untested and put together after 5 min of Googling, so if somehting is wrong, feel free to comment and I'll amend it. It should be fairly close and is mostly for explaining the concept instead of a pure copy/paste solution
Register a new shortcode:
add_shortcode('my_awesome_content', 'my_awesome_content_func');
Create the callback function:
Here we've added $atts which will contain our attribute (the post id):
function my_awesome_content_func($atts = [])
{
$postId = $atts['postid'] ?? null;
if ($postId === null) {
// We got no id so let's bail
return null;
}
$post = get_post($postId);
if (!$post) {
// We didn't find any post with the id so again, let's bail
return null;
}
$group_id = get_field( 'link_number' , $post->ID );
$content = $post->content;
if( $group_id ) {
$content .= do_shortcode( '[checkout_button class="button" level="' . $group_id . '" text="Order"]' );
}
return $content;
}
Usage:
Now you should be able to call it like this:
echo do_shortcode('[my_awesome_content postid="' . $post->ID . '"]');
You can just embed you code below in the above filter with some modifications:
function displaycontent($content) {
if (is_singular('custom_post_type')) {
$post_id = get_queried_object_id();
$group_id = get_field('link_number', $post_id);
$aftercontent = '';
if ($group_id)
$aftercontent = do_shortcode('[checkout_button class="button" level="'.$group_id. '" text="Order"]');
$fullcontent = $content.$aftercontent;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter('the_content', 'displaycontent');
OK, thanks everyone, I Got it! Here is the code if anyone would have same problem:
function displaycontent($content) {
if(is_singular( 'custom_post_type' )) {
$group_id = get_field('link_number');
$aftercontent = do_shortcode( '[checkout_button class="button" level="' . $group_id . '" text="Order"]' );
$fullcontent = $beforecontent . $content . $aftercontent;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter('the_content', 'displaycontent');

Return ACF field as a Shortcode using Wordpress Functions.php file

How do I use an Advanced Custom Field as a Shortcode. Ive used the following code in the Wordpress functions.php file but no luck.
Here is my Code:
function location_date_func( $atts ){
return "<?php the_field('location_date', 658); ?>";
}
add_shortcode( 'location_date', 'location_date_func' );
You need to register the shortcode properly, and make it return the data to display, not return a string with php code in it:
function location_date_func( $atts ){
//return string, dont echo it, so use get_field, not the_field
return get_field('location_date', 658);
}
//create function to register shortcode
function register_shortcodes(){
add_shortcode( 'location_date', 'location_date_func' );
}
// hook register function into wordpress init
add_action( 'init', 'register_shortcodes');
Or if you are using php 5.3+, you can use anonomous functions to acheive the same result:
add_action('init', function(){
add_shortcode('location_date', function(){
return get_field('location_date', 658);
});
});
Got it to work!
function location_date_func( $atts ){
return apply_filters( 'the_content', get_post_field( 'location_details', 658 ) );
}
add_shortcode( 'location_date_sc', 'location_date_func' );
If you want to return the value of an ACF field using the_field(), there is already a built in shortcode to do that.
[acf field="location_date" post_id="658"]
If you would like to reproduce it using the [location_date] shortcode, you need to use get_field() to return rather than echo the value. Syntax-wise, the only problem with your code is that you do not need the double quotes or <?php tags, since it should already be inside a PHP block. It will be functionally the same as the [acf] shortcode, but does not accept the post_id argument. This example will be hard coded to post ID 658 unless you modify it to accept an ID as part of the $atts or use the global $post;
function location_date_func( $atts ){
return get_field( 'location_date', 658 );
}
add_shortcode( 'location_date', 'location_date_func' );
add_shortcode('location_start_your_application_group', 'start_your_application_group');
function start_your_application_group() {
$start_your_application_group = '';
$start_your_application_group .= '<section class="start-your-application">';
if ( have_rows( 'start_your_application_group', 'option' ) ) :
while ( have_rows( 'start_your_application_group', 'option' ) ) : the_row();
$heading = get_sub_field( 'heading' );
$content = get_sub_field( 'content' );
if ( $heading !== '' ) {
$start_your_application_group .= '<h3 class="start-your-application__heading">' . $heading . '</h3>';
}
if ( $content !== '' ) {
$start_your_application_group .= '<div class="start-your-application__content">' . $content . '</div>';
}
$image = get_sub_field( 'image' );
if ( $image ) {
$start_your_application_group .= '<div class="start-your-application__image-container"><img class="start-your-application__image" src="' . $image['url'] .'" alt="' . $image['alt'] . '" /></div>';
}
endwhile;
endif;
$start_your_application_group .= '</section>';
return $start_your_application_group;
}

How do I apply this conditional logic to a complicated page title/subtitle function?

I'm using a few functions and conditionals to display page titles and a subpage title field on all pages. Instead of adding this code to the page template, I want to add it to my functions file so I can easily add too the conditional if I’d like. I found this code which successfully adds something to the ‘the_title’ function if on a page, but I’m having trouble applying my code and conditions to it.
Here's my conditional code currently on the page template:
<?php $subtitle = get_post_meta($post->ID, 'html5blank_webo_subtitle', true); ?>
<?php if ( $subtitle ) : ?>
<div class="page-title"><?php the_title(); ?></div>
<h1 class="subtitle"><?php the_subtitle(); ?></h1>
<?php else : ?>
<h1 class="page-title"><?php the_title(); ?></h1>
<?php endif; ?>
Here's the code I found and am trying to apply the above code to:
add_filter('the_title', 'new_title', 10, 2);
function new_title($title, $id) {
if('page' == get_post_type($id))
$title = 'Application has been updated to v'.$title;
return $title;
}
And here's my failed attempt:
add_filter('the_title', 'new_title', 10, 2);
function new_title($title, $id) {
if('page' == get_post_type($id))
$subtitle = get_post_meta($post->ID, 'html5blank_webo_subtitle', true);
if ( $subtitle ) :
$title = '<div class="page-title">' . the_title() . '</div><h1 class="subtitle">' . the_subtitle() . '</h1>';
else :
$title = '<h1 class="page-title">' . the_title() . '</h1>';
endif;
return $title;
}
Final working code:
// Swapping the default 'the_title' with our subtitle on pages
function subtitle_title( $title, $id ) {
if( !in_the_loop() || !is_page() ) // If not in the loop or on a page, default the the regular 'the_title'
return $title;
$subtitle = get_post_meta( $id, 'html5blank_webo_subtitle', true );
if ( $subtitle ) :
$title = '<div class="page-title">'
. $title
. '</div><h1 class="subtitle">'
. $subtitle
. '</h1>';
else :
$title = '<h1 class="page-title">' . $title . '</h1>';
endif;
return $title;
}
add_filter( 'the_title', 'subtitle_title', 10, 2 );
WordPress doesn't have the function the_subtitle, but normally all functions starting with the_* will print the value and functions starting with get_the_* will return the value.
Your code entered an infinite loop, because you're calling the_title (should be get_the_title) inside a function that's filtering the_title. It could be fixed removing and adding the filter inside the callback, but that's not needed, as the title is already available.
And also, you're using $post without it being defined, and you don't need it, the post ID is already available too.
Finally, I'm thinking that you're confusing this the_subtitle() function with the value your getting from $subtitle=get_post_meta().
add_filter( 'the_title', 'new_title', 10, 2 );
function new_title( $title, $id )
{
# http://codex.wordpress.org/Conditional_Tags
if( !is_page_template( 'about.php' ) )
return $title;
if( 'page' == get_post_type( $id ) )
$subtitle = get_post_meta( $id, 'html5blank_webo_subtitle', true );
if ( $subtitle ) :
$title = '<div class="page-title">'
. $title
. '</div><h1 class="subtitle">'
. $subtitle
. '</h1>';
else :
$title = '<h1 class="page-title">' . $title . '</h1>';
endif;
return $title;
}

Categories