• Resolved mhmuc

    (@mhmuc)


    Hi everyone,

    I have a plugin, that uses CF7-Form-Data for a calculation and I want to show the dynamic results in a table with shortcodes. The shortcode-values are displayed, but I don’t manage to change the shortcodes. From my understanding the call-back-function should be called, when the shortcode is rendered on the page. It seems to be called before the rest of the functions were called.

    Any idea? Thanks a million…

    <?php
    
    //Grundsätzliche Initialisierung des Plugins
    function XXX_betriebskosten() {
    	//Übersetzung
          betriebskosten_shortcodes();
    }
    
    /*SETTINGS */
    $betriebskosten_formid = 3027;
    
    $betriebskosten_shortcodes = array(
    'betriebstage' => 1,
    'spuelleistung' => 2,
    'betriebskosten' => 3
    );
    
    //Alle Aktionen nur ausführen, wenn es sich um das Betriebskosten-Form handelt. 
    add_action( 'wpcf7_contact_form', 'action_wpcf7_contact_form', 1, 1 ); 
    
    //Form-ID prüfen und nur für Betriebskosten-Form die weiteren Filter laden, damit die Performance der Website nicht leidet. . 
        function action_wpcf7_contact_form( $instance ) { 
            //Wenn es die richtige Form-ID ist, können alle weiteren Filter ausgeführt werden. 
            //Kommt oben aus den Settings.
            global $betriebskosten_formid;
            
            if ( $instance->ID() == $betriebskosten_formid ) {
                //Filter ruft Funktion auf, die automatisch das data:maschinen füllt
                add_filter('wpcf7_form_tag_data_option', 'get_machine_select_values' , 10, 3);
                
                //Wenn Action gesetzt ist, wurde das Formular abgeschickt, die Validierung sollte erfolgt sein und die Berechnung kann beginnen
                if ( isset( $_POST["action"] ) ) {
                    vergleichStarten();
                    
                }
            }
            
         }; 
    
    function vergleichStarten() {
        //Holt die in der Funktion getPostedData ausgewählten Daten, die vom Formular übertragen wurden. 
        /* */
        changeBetriebsKostenShortcode('betriebskosten','1234');
    
        
         
    }
    
    function betriebskosten_shortcodes()   {
        global $betriebskosten_shortcodes;
        foreach ( $betriebskosten_shortcodes as $shortcodekey => $shortcodevalue) {
            add_shortcode($shortcodekey,'shortcodeOutput');
            
        }
    }
    
    function changeBetriebsKostenShortcode($key, $value)    {
           global $betriebskosten_shortcodes;
           $betriebskosten_shortcodes[$key] = $value;
           
    }
    
    function shortcodeOutput( $atts = [], $content = null, $tag) {
        global $betriebskosten_shortcodes;
        $content = "Es wurde ". $tag . "aufgerufen: " . $betriebskosten_shortcodes[$tag];
        return $content;
    }
    
                     
    add_action('init', 'XXX_betriebskosten');
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    It’s a little hard to follow what you’re doing. Shortcode processing is a single pass operation, hence if you have shortcodes within shortcode output, they are not automatically processed. You’d need to explicitly run such internal shortcodes through do_shortcode() to get them processed.

    Shortcode callbacks are passed two values, attributes and content. In shortcodeOutput() you’re collecting three. $tags isn’t passed as you are expecting. Such a value should be passed via shortcode attributes, something like "[my_shortcode tags=\"$tags\"]".

    Thread Starter mhmuc

    (@mhmuc)

    Hi bcworkz,

    thank you for your reply.

    What I basically trying to do is use a shortcode in the frontend e.g. [TEST], that is dynamically filled during plugin-runtime.

    I get it to work if I define the shortcode in the init-part of the plugin, but I can’t change the content in another function of the plugin. This must be possible

    Moderator bcworkz

    (@bcworkz)

    When the [TEST] callback executes, it’s not going to know about other functions trying to change what it does unless those other functions save what they want to do someplace where the [TEST] callback can access it when it executes. Someplace such as a global value or DB content.

    While the other functions could conceivably be called from the [TEST] callback, their declarations are essentially static. The only way for them to behave dynamically is to have them also access a global or DB content.

    A typical example of a shortcode showing dynamic content is one that displays the latest post on a specific page named “Post of the Day” or similar. The shortcode callback queries for the latest post and returns appropriate HTML. Some other function, such as a cron event handler could automatically create a new post daily. Maybe it posts “lucky” (random) Lottery number picks.

    Thus one function, the cron event handler, dynamically affects shortcode output. But it does so via the DB as an intermediary. It cannot directly affect what the shortcode callback does because they execute at different times.

    Thread Starter mhmuc

    (@mhmuc)

    Hi everyone,

    the problem is solved.
    I executed the function that changes the short-code-content within the action of the contact form. Somehow that ran at a different stage so it was not executed in the right time.
    I solved it differently now…

    THank you very much for your help!

    add_action( 'wpcf7_contact_form', 'action_wpcf7_contact_form', 10, 1 );

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Change shortcode value within plugin’ is closed to new replies.