• Ok so I can’t, for some reason figure out a good way to do the following:

    What I’m trying to do is write PHP in my posts that will execute only once when the post is published. I want that PHP to output some static HTML.

    I’m using Exec PHP.

    Ie.

    <?php function myfunction{}>
    would output
    “The text I require”
    in STATIC html.

    I’m trying to write a script that grabs a non-dynamic stock quote at the top of each page.

    Any help would be much appreciated.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You can do this using the transition_post_status action.

    https://codex.www.ads-software.com/Post_Status_Transitions

    This action is not well documented, but you can use it to execute a block of code whenever a post transitions from one status to another (like going from ‘draft’ to ‘publish’). Something like this:

    function insert_stock_quote($new_status, $old_status, $post) {
    	if ( 'publish' == $new_status && !in_array($old_status, array( 'publish', 'private', 'trash' )) ) {
    		[code to add stock quote goes here]
    	}
    }
    add_action('transition_post_status', 'insert_stock quote', 10, 3);
    Thread Starter johndalton80

    (@johndalton80)

    Wouldn’t that only display text when I update the post then?

    For example:

    function insert_stock_quote($new_status, $old_status, $post) {
    if ( ‘publish’ == $new_status && !in_array($old_status, array( ‘publish’, ‘private’, ‘trash’ )) ) {
    echo get_quote();
    }
    }
    add_action(‘transition_post_status’, ‘insert_stock quote’, 10, 3);

    Also where would I write this code to have it generate static content?

    I’m a Newb at this so Thanks for your patience.

    John

    Try adding it to your theme’s functions.php file.

    Thread Starter johndalton80

    (@johndalton80)

    Ok so I added this to functions, notice that I just used an echo for testing.

    function insert_stock_quote($new_status, $old_status, $post) {
    	if ( 'publish' == $new_status && !in_array($old_status, array( 'publish', 'private', 'trash' )) ) {
    		echo 'this goes at the top of the post';
    	}
    }
    add_action('transition_post_status', 'insert_stock quote', 10, 3);

    and got these errors when I published or when I hit upgrade, and it doesn’t work:

    Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'insert_stock quote' was given in /home/bhgadmin/public_html/vannet/wp-includes/plugin.php on line 395
    
    Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'insert_stock quote' was given in /home/bhgadmin/public_html/vannet/wp-includes/plugin.php on line 395
    
    Warning: Cannot modify header information - headers already sent by (output started at /home/bhgadmin/public_html/vannet/wp-includes/plugin.php:395) in /home/bhgadmin/public_html/vannet/wp-includes/pluggable.php on line 890

    The first two errors are the result of a typo (my mistake). There should be an underscore between ‘stock’ and ‘quote’ in the function name:

    add_action('transition_post_status', 'insert_stock_quote', 10, 3);

    As for the ‘headers already sent’ error, you can’t just echo the text to the screen like that. You need to write a function that inserts the text into the WordPress database. My only purpose in posting that code snippet was to show how you could plug the function into WordPress using an appropriate action.

    Thread Starter johndalton80

    (@johndalton80)

    Ok thanks, that makes sense.

    Could you perhaps direct me to how I might learn to do that? Or post a sample? (writing to the WP database).

    Thanks for your help, it’s MUCH appreciated.

    IH

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Executing PHP in a Post to achieve static output’ is closed to new replies.