$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'); ?>theme development - Storing the contents from txt file into The_Contents"|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)

theme development - Storing the contents from txt file into The_Contents"

matteradmin11PV0评论

Currently I am trying to fetch data from an external page, which I have stored via a custom field and store it into the top portion of my WordPress the_content when the post is saved.

The steps are accordingly.

Save URL into custom field.

Save Post

Behind the scenes while the post is publishing to database, grab the contents from the url, and set them into the_content on WordPress, without removing the existing the_content data.

Whats the best way to do this?

Currently I am trying to fetch data from an external page, which I have stored via a custom field and store it into the top portion of my WordPress the_content when the post is saved.

The steps are accordingly.

Save URL into custom field.

Save Post

Behind the scenes while the post is publishing to database, grab the contents from the url, and set them into the_content on WordPress, without removing the existing the_content data.

Whats the best way to do this?

Share Improve this question asked Feb 18, 2019 at 20:46 user159405user159405 1
  • 1 What kind of "data" .. HTML? JSON? Please be more specific – sMyles Commented Feb 18, 2019 at 22:29
Add a comment  | 

1 Answer 1

Reset to default 0

What kind of data? HTML? JSON? You did not provide enough specifics for us to actually help you with this .. including what kind of Post Type.

There are numerous filters you can use for this, this is a ROUGH markup of PHP to do something like this before the data is inserted (based on limited information provided)

add_filter( 'wp_insert_post_data', 'insert_custom_data_before_content', 10, 2 );

function insert_custom_data_before_content( $data, $postarr ){

    $external_url = sanitize_text_field( $_POST['external_url'] );
    $request = wp_remote_get( $external_url );

    if( ! is_wp_error( $request ) && wp_remote_retrieve_response_code( $response) === 200 ){

        if( $response = wp_remote_retrieve_body( $request ) ){
            $data['post_content'] = wp_kses( $response, true ) . $data['post_content'];
        }

    }

    return $data;
}

This is assuming you have already added your own custom field with a name attribute of external_url (which you should add your own PHP to check that value)

There's also save_post when a post is saved (after being updated) which also passes an argument if it was an update or not.

You also need to keep in mind handling data sanitation, ESPECIALLY if you're pulling data from an external URL to insert into your database .. that opens up all kinds of potential security concerns.

https://codex.wordpress/Validating_Sanitizing_and_Escaping_User_Data

Post a comment

comment list (0)

  1. No comments so far