functions - How to output a PHP file values by shortcode?

admin2025-06-02  0

I'm trying to output the values of two custom fields created by ACF plugin in a custom post type. I actually need this to be displayed in a popup window. wordpress popup plugins don't support php code, only shortcodes can be called from the content editor. So I'm trying to create a shortcode to be used in the editor of the popup to display the values of the custom fields. I know that we can generate shortcode [cite] using this code in theme functions.php

function cite_shortcode() {
}
add_shortcode( 'cite', 'cite_shortcode' );

But i couldn't figure out how to add the php code to that code. I tried to do something like:

function cite_shortcode() {
<div>
<?php
$object_terms = wp_get_object_terms( $post->ID, 'issue', array( 'fields' => 'all' ) );
if ( $object_terms ) {
    $res = '';
    foreach ( $object_terms as $term ) {
        if ( $term->parent ) { 
            $res .=  $term->name . ', '; 
        }
    }
    echo rtrim($res,' ,');  
}
?>), pp: <?php the_field('first_page'); ?>-<?php the_field('last_page'); ?>
</div>
}
add_shortcode( 'cite', 'cite_shortcode' );

But it didn't work. it shows:

Parse error: syntax error, unexpected

So, my questions are:

  1. how can i make that code work?
  2. is it possible to put the php code in cite.php file and output its values via a shortcode generated in functions.php? and how to do that?

Best regards

I'm trying to output the values of two custom fields created by ACF plugin in a custom post type. I actually need this to be displayed in a popup window. wordpress popup plugins don't support php code, only shortcodes can be called from the content editor. So I'm trying to create a shortcode to be used in the editor of the popup to display the values of the custom fields. I know that we can generate shortcode [cite] using this code in theme functions.php

function cite_shortcode() {
}
add_shortcode( 'cite', 'cite_shortcode' );

But i couldn't figure out how to add the php code to that code. I tried to do something like:

function cite_shortcode() {
<div>
<?php
$object_terms = wp_get_object_terms( $post->ID, 'issue', array( 'fields' => 'all' ) );
if ( $object_terms ) {
    $res = '';
    foreach ( $object_terms as $term ) {
        if ( $term->parent ) { 
            $res .=  $term->name . ', '; 
        }
    }
    echo rtrim($res,' ,');  
}
?>), pp: <?php the_field('first_page'); ?>-<?php the_field('last_page'); ?>
</div>
}
add_shortcode( 'cite', 'cite_shortcode' );

But it didn't work. it shows:

Parse error: syntax error, unexpected

So, my questions are:

  1. how can i make that code work?
  2. is it possible to put the php code in cite.php file and output its values via a shortcode generated in functions.php? and how to do that?

Best regards

Share Improve this question asked Sep 12, 2015 at 12:52 nisrnisr 714 silver badges17 bronze badges 2
  • 2 Please read up on how to change between php and html, also, read up on shortcodes. Shortcode should never echo output, it should return output – Pieter Goosen Commented Sep 12, 2015 at 12:59
  • You also have undefined variables. Turn debug on – Pieter Goosen Commented Sep 12, 2015 at 13:00
Add a comment  | 

3 Answers 3

Reset to default 1

We've built shortcodes in the past using concatenating variables.

To paraphrase the link above, you should output your PHP as such.

 $output = '<p>';
 $output .= '<strong>' . $content . '</strong>';
 $output .= '</p>';
 return $output;

Note, see the .= variable concatenation.

Just in case anyone will search for this in the future...

The proper way to output php value through a shortcode is this one:

add_shortcode('shortcode_name', 'shortcode_callback');
function shortcode_callback( $atts = array(), $content = null ) {

   $output = "Add your PHP here!!!";

   return $output;

}

Greetings from sunny Greece!!!

  1. how can i make that code work?
function cite_shortcode($atts) {
    $output = '<div>';

    $object_terms = wp_get_object_terms( $post->ID, 'issue', array( 'fields' => 'all' ) );

    if ( $object_terms ) {

        $res = '';

        foreach ( $object_terms as $term ) {

            if ( $term->parent ) { 
                $res .=  $term->name . ', '; 
            }

        }

        $output .=  rtrim($res,' ,');  
    }

    $output .= 'pp: '.get_the_field('first_page') . '-' . get_the_field('last_page');

    $output .= '</div>';

    return $output ;
}

add_shortcode( 'cite', 'cite_shortcode' );
  1. is it possible to put the php code in cite.php file and output its values via a shortcode generated in functions.php? and how to do that?

In this case, you can inclde your php file within short code, something similar to this code

function cite_shortcode($atts) {
    $output = '';

    ob_start();

    include "yourphpfile.php"; // Replace with exact location of php file 


    $output .= ob_get_clean();

    return $output ;
}

add_shortcode( 'cite', 'cite_shortcode' );

I hope this helps.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748822305a314012.html

最新回复(0)