Using PHP Wordpress variable do_shortcode for dynamic content - php

I'm building a Wordpress site and my aim to have users be able to place custom shortcodes in the backend of Wordpress on blog posts and have that content display on the site.
I'm using a simple text field Advanced Custom Field to do this (below.) but am struggling to integrate this with the do_shortcode variable.
Any help would be greatly appreciated, thanks.
Get Advanced Custom Field input to display as plaintext
<?php if( get_field('brand_video') ):?>
<?php the_field('brand_video');?>
<?php endif;?>
Get Wordpress to output video
<?php echo do_shortcode("[video id=204]"); ?>
My attempt of combining the two
<?php if( get_field('brand_video') ):?>
<?php echo do_shortcode(the_field('brand_video'));?>
<?php endif;?>

In do_shortcode replace the_field with get_field.
the_field is return a value.
get_field is print a value.
<?php if( get_field('brand_video') ):?>
<?php echo do_shortcode(get_field('brand_video'));?>
<?php endif;?>

Related

Wordpress function get_post_gallery() does not register post's gallery

I want to extract the images contained in a gallery block while maintaining their correct order. Since get_children() and get_attached_media() do not seem to register when the image order is changed in wp-admin, I'm trying to use get_post_gallery() instead.
My problem is that the function returns false, even though the post does have a gallery.
I tried both the example and the plain usage from Codex. Currently, my entire single.php looks like this:
<?php
get_header(); //html head etc
if (have_posts()): while (have_posts()) : the_post(); //the loop
if ( get_post_gallery() ) :
echo get_post_gallery();
else :
echo (the_ID() . " has no gallery.");
endif;
endwhile;
endif;
?>
… which results in "ID has no gallery" every time.
However, the output of print_r($post->post_content); does include the following, which seems to confirm that there is in a fact a gallery:
<!-- wp:gallery {"ids":[80,81,82]} -->
<figure class="wp-block-gallery columns-3 is-cropped">
<ul class ="blocks-gallery-grid">
<!-- … -->
I'm also attaching a screenshot from wp-admin to make sure I don't misunderstand what constitutes a gallery.
get_post_gallery() works only for native galleries which have been created in classig WYSIWYG editor. You can find more about it there. Hovewer, if you create Gallery via Gutenberg, it create the whole HTML code instead of classic editor gallery which create shortcode like this: [gallery ids="400097,400052,400051"] . Functions get_post_gallery(), get_post_galleries() and get_post_gallery_images() works only on classic galleries added via native shortcode.
Are you only trying to display the gallery block as opposed to all content? If so, maybe try this?
<?php
get_header(); //html head etc
if (have_posts()): while (have_posts()) : the_post(); //the loop
if (has_block('gallery', $post->post_content)) {
echo 'yes, there is a gallery';
$post_blocks = parse_blocks($post->post_content);
foreach ($post_blocks as $post_block){
if ($post_block['blockName'] == 'core/gallery'){
echo do_shortcode( $post_block['innerHTML'] );
}
}
}
// if there is not a gallery block do this
else {
echo 'no gallery';
}
endwhile;
endif;
get_footer();
?>
I got some inspiration from reading this post btw.

Show ACF in customer account dashboard - Woocommerce

I would like to show a custom welcome message for each of my registered shop customers. Something like "Welcome CUSTOMERNAME! You have been with us for one year now. To say thanks, we are giving you a discount on everything in our shop of 10%."
I have created a custom field welcome_message with the plugin Advanced Custom Fields and would like to show the value of it in the account dashboard of my customers at frontend.
At the backend, I am showing the textarea at the user profile page and I am adding the field with this code:
$message = get_field( 'welcome_message' );
echo esc_attr( $message );
This code is placed here: /wp-content/themes/flatsome-child/woocommerce/myaccount/dashboard.php
But somehow the value is not coming up. I have also tried it with simply the_field('welcome_message'); or to add [acf field="{$welcome_message}"] at the account page in WordPress but that didn't work either. Somehow the value is not coming through.
I hope someone could help me out a litte.
Thanks in advance.
Best regards
It's necessary that get_field function is connected to user.
Eg:
<?php
$variable = get_field('field_name', 'user_1');
?>
In case of repeater field:
<?php if( have_rows('repeater', 'user_1') ): ?>
<ul>
<?php while( have_rows('repeater', 'user_1') ): the_row(); ?>
<li><?php the_sub_field('title'); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>

function for post from category in wordpress

I have the following function in functions.php page
function viewpost($num)
{
echo $num;
query_posts('order=dsc&cat=$num & showposts=2');
while (have_posts()) : the_post();
?> <span> <?php the_title(); ?>
<?Php
echo get_the_post_thumbnail();
the_excerpt();
?>
<?Php
endwhile;
wp_reset_query();
}
When I call viewpost function for values of viewpost(1)(to view post from category one ) it shows correct values, but when I put the same function again viewpost(2) (to view post from category 2) it shows the previous function values i.e. from category. What can I do to get the post from different categories by changing the passing value
Without trying your code, I think the most likely problem is you're using single quotes. Variable names won't get expanded to their values. See this answer.
Try
query_posts("order=dsc&cat=$num&showposts=2");
instead of
query_posts('order=dsc&cat=$num & showposts=2');
This might be worth a read too. Using query_posts is normally not recommended.

Using Advanced Custom Fields and Contact Form 7 to display a form

I want my users to be able to put a Contact Form 7 shortcode into a custom field in the Wordpress editor. I've created the custom field using ACF and I can pull the value onto the page, but when I try to include it in the shortcode, it comes back with a 404.
This code:
<?php echo do_shortcode(get_field('contact_form_shortcode')); ?>
Returns:
[contact-form-7 404 "Not Found"]
If I create a variable out of the value like this:
<?php
$formCode = get_field('contact_form_shortcode');
echo $formCode;
?>
The echo returns:
[contact-form-7 id="473" title="Learn More Form"]
But I get the same 404 after putting that value into the echo do_shortcode function list this:
<?php echo do_shortcode($formCode); ?>
What am I missing?
To do it With ACF pro plugin and without other extra plugins.
Create a relation field ( example: contact_form )
add the below code into your page loop:
<?php $posts = get_field('contact_form');
if( $posts ):
foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT)
$cf7_id= $p->ID;
echo do_shortcode( '[contact-form-7 id="'.$cf7_id.'" ]' );
endforeach;
endif; ?>
I was able to resolve this issue by using the technique I discussed in my comment above. By using the WYSWIG field set to 'Run filter "the_content"' I'm able to pull the field value in the way I want it. The only drawback is that users could type something else in there besides a form shortcode.
Here's my final code:
<?php
if (get_field('contact_form_shortcode')):
echo get_field('contact_form_shortcode');
else:
echo do_shortcode('[contact-form-7 id="473" title="Learn More Form"]');
endif;
?>
Instead of using a WYSIWYG field, you can simply set a Text field to "Convert HTML into tags" under the Formatting setting on the field. This will stop the 404 errors from CF7 and properly process the form. The WYSIWYG fields tend to be too hard to control from bad user input.
Here's my solution:
Create a "Post Object" type Field which will returns the post ID.
the Filter by Post Type for this field should be sett to "Contact Form" and Filter by Taxonomy leave empty.
Then in page template: (php)
<?php $contact_form = get_field('contact_form'); ?>
HTML:
<div class="col-12 col-md-8 offset-md-2">
<?php echo do_shortcode('[contact-form-7 id="'. $contact_form .'"]'); ?>
</div>
acf field screenshot
Another Solution :
1) Install https://github.com/taylormsj/acf-cf7
Installation
> 1 Copy the acf-cf7 folder into your wp-content/plugins folder
> 2 Activate the Contact Form 7 plugin via the plugins admin page
> 3 Create a new field via ACF and select the Contact Form 7 type
> 4 Please refer to the description for more info regarding the field type settings
2) Insert following code into template :
<?php if( get_field('field_acf') ):
the_field('field_acf');
endif; ?>
This applies to create a contact form 7 in a modal or Pop Up, multiple forms on the same page.
To paste CF7 shortcode (or any other generated shortcode) into ACF field as you were asking, just use the #armadadrive solution, which is exactly what question was about and it works.
create ACF text field with some name, eg hero_form
paste your shortcode into it
display contents like so <?php echo do_shortcode(get_field('hero_form')); ?>
Thanks #armadadrive
Here you can try please check the code for the shortcode below:
<?php echo do_shortcode(get_field('YourACFfieldNameHere')); ?>
I tried it on my website and it works.

Get ID for static homepage, not post

I am starting to write a custom theme for Wordpress. I have created a new page called 'Home' and set the front page to be a static page, selecting 'Home'. I want this page to show all posts with a category of 'news' plus a couple of images.
I then added front-page.php with the following:
<?php get_header(); ?>
<div class='detail'>
<?php if ( have_posts() ) {
query_posts( 'category_name=news' );
while ( have_posts() ) : the_post(); ?>
<h4><?php the_date('d/m/Y'); ?> - <?php the_title(); ?></h4>
<div class='post'><?php the_content(); ?></div>
<?php endwhile; }?>
</div>
<?php get_footer(); ?>
I've uploaded a couple of images and attached them to the 'Home' page. I now want to get the URL for the attached images. I've tried something like:
$images =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . get_the_ID());
but this only returns the ID of the most recent post being shown, even if I put the above code outside the loop. If I remove the static front page and navigate to the Home page then I get the correct results, any ideas how I can get the images for the Home page when I use it as a static page?
Forgive me if this is simple, first foray into PHP and wordpress development
I think your issue is that you're using get_the_ID(); outside of The Loop. the_ID(); and get_the_ID(); grab the current post ID from the loop; if used outside of The Loop, all you'll get is the last one. See: http://codex.wordpress.org/Function_Reference/get_the_ID
To get the ID of the current page, try:
$page=get_page_by_title($page_name);
$images =& get_children('post_type=attachment&post_mime_type=image&post_parent='.$page->ID);
If that doesn't work, there's a function at http://wordpress.org/support/topic/how-to-get-page-id-using-php?replies=5 (Where I found the above code) that does the same thing.
Hope this helps!

Categories