$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>functions - How to output a PHP file values by shortcode?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far