Wordpress modifies content when importing post - php

I use the PHP script below to import new posts into Wordpress.
require_once("wp-load.php");
$url = 'https://somejsonfeed.com'; // path to the JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$flights = json_decode($data, true); // decode the JSON feed
foreach ($flights['response'] as $item) {
$title = $item['flight']['iata_number'];
if ( ! is_admin() ) {
require_once( ABSPATH . 'wp-admin/includes/post.php' );
}
if ( get_page_by_title( $title, OBJECT, 'custom-post-name' ) == null ) {
// Insert post
$new_post = array(
'post_title' => $title,
'post_content' => '[jsoncontentimporterpro url=yes url=https://someapiwithakey123abc&iata_code=<cf_wpcf-arrival-airport\> value&type=something]',
'post_status' => 'draft',
'post_author' => 1,
'post_type' => 'custom-post-name'
);
// Insert post
$post_id = wp_insert_post($new_post);
// Insert post meta if available
add_post_meta( $post_id, 'meta_key', 'meta_value' );
}
}else{
// Update meta value
update_post_meta($get_page->ID, 'my_key', 'meta_value');
}
But after importing the content it removes <cf_wpcf-arrival-airport\> and shows the content below in the Wordpress WYSIWYG editor.
[jsoncontentimporterpro url=yes url=https://someapiwithakey123abc&iata_code= value&type=something]
Any idea how I can fix this?

No perfect solution, but a work around.
Used the https://nl.wordpress.org/plugins/insert-html-snippet/ plugin and imported the post-content value into a snippet. Then I used the snippet in the file above.

Related

ACF Wordpress display an image shortcode problem

I am trying to display an image on the homepage using a shortcode and ACF.
The problem I am having is that the code is fetching the data for the correct post, but it is displaying a line of text of the image address, rather than an image. When I view the page source it appears the the img tag is not working.
This is the code I have. Any advice would be greatly appreciated.
function grab_home_image_init(){
add_shortcode( 'grab_home_image', 'grab_home_image_cb' );
}
add_action('init', 'grab_home_image_init');
function grab_home_image_cb() {
extract( shortcode_atts(
array(
'numberposts' => 1,
'post_type' => 'project',
),
$atts,
'grab_home_image'
) );
$args = array (
'post_type' => $post_type,
'numberposts' => $numberposts,
);
$ML_home_image = get_posts( $args );
if( ! empty( $ML_home_image ) ){
foreach ( $ML_home_image as $p ){
$output = '<img src="' . the_field('main_image',$p->ID) . '">';
}
}
return $output ?? '<strong>Error</strong>';
}

Check if title exists in wordpress database before import

I am using a json feed to import new posts. This works as expected, but I am not able to prevent importing duplicated posts. When I run the script twice it also imports the posts twice.
How can I check the wordpress database to prevent importing the same posts.
require_once("wp-load.php");
$url = 'https://somejsonfeed.com'; // path to the JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$flights = json_decode($data, true); // decode the JSON feed
foreach ($flights['response'] as $item) {
$title = $item['flight']['iata_number'];
// Check if already exists
if ( get_page_by_title( $title ) == null ) {
// Insert post
$new_post = array(
'post_title' => $title,
'post_content' => $title,
'post_status' => 'draft',
'post_author' => 1,
'post_type' => 'post'
);
// Insert post
$post_id = wp_insert_post($new_post);
// Insert post meta if available
add_post_meta( $post_id, 'meta_key', 'meta_value' );
// Uncomment to check if meta key is added
// $get_meta_value = get_post_meta( $post_id, 'meta_key', true );
// echo "<pre>";
// print_r($get_meta_value);
}
}else{
// Update meta value
update_post_meta($get_page->ID, 'my_key', 'meta_value');
// Uncomment to check if meta key is added
// $get_meta_value = get_post_meta( $get_page->ID, 'meta_key', true );
// echo "<pre>";
// print_r($get_meta_value);
}
What works well for me is post_exists($title). Returns the post ID, so in your code, I would use
$current_post_id = post_exists($title);
if( 0 === $current_post_id) {
`enter code here`add_post_meta( $post_id, 'meta_key', 'meta_value' );
}
One thing I notice is that you have:
'meta_key', 'meta_value', so of course, I copied that part, but you would need to replace whatever meta_key and meta_value.
I hope this helps.
Bruce
Solved by using the code below
require wp-load.php to use built-in WordPress functions
require_once("wp-load.php");
$url = 'https://somejsonfeed.com'; // path to the JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$flights = json_decode($data, true); // decode the JSON feed
foreach ($flights['response'] as $item) {
$title = $item['flight']['iata_number'];
if ( ! is_admin() ) {
require_once( ABSPATH . 'wp-admin/includes/post.php' );
}
// change custom-post-name in post type
if ( get_page_by_title( $title, OBJECT, 'custom-post-name' ) == null ) {
// Insert post
$new_post = array(
'post_title' => $title,
'post_content' => $title,
'post_status' => 'draft',
'post_author' => 1,
'post_type' => 'post'
);
// Insert post
$post_id = wp_insert_post($new_post);
// Insert post meta if available
add_post_meta( $post_id, 'meta_key', 'meta_value' );
// Uncomment to check if meta key is added
// $get_meta_value = get_post_meta( $post_id, 'meta_key', true );
// echo "<pre>";
// print_r($get_meta_value);
}
}else{
// Update meta value
update_post_meta($get_page->ID, 'my_key', 'meta_value');
// Uncomment to check if meta key is added
// $get_meta_value = get_post_meta( $get_page->ID, 'meta_key', true );
// echo "<pre>";
// print_r($get_meta_value);
}

Adding tag input in post form

So, I have following form in my plugin for post upload (wordpress):
if(empty($_POST['post_id'])){
$post = array(
'post_content' => 'Dummy Content',
'post_title' => 'Dummy Title',
'post_status' => 'inherit',
'post_type' => 'post'
);
$post_id = wp_insert_post( $post, true );
}else{
$post_id = $_POST['post_id'];
}
$upload_dir = wp_upload_dir();
$files = $_FILES['files'];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$filename = rand() . '' . $files['name'][$key];
$file = array(
//Here goes other irrelevant code
);
$upload_overrides = array( 'test_form' => false );
$image_path[] = array(
'image_path' => $upload_dir['url'] . '/' . $filename,
'meta_key' => 'upload_image',
'meta_value' => $upload_dir['url'] . '/' . $filename,
'post_id' => $post_id
);
add_post_meta($post_id, 'upload_image', $upload_dir['url'] . '/' . $filename );
}
}
echo json_encode($image_path);
exit;
Above function adds an image as a post with dummy contents such as title/description etc.
Now, if I want to update the content, then I can do so by following:
global $wpdb;
$tags = $_POST['rh_tags'];
if(!empty($_POST['post_id'])){
$data = array(
'post_title' => $_POST['title'],
'post_content' => $_POST['content'],
'tags_input' => $tags,
'post_status' => 'publish'
);
$where = array('ID' => $_POST['post_id']);
$update = $wpdb->update($wpdb->prefix . 'posts' , $data, $where, $format = null, $where_format = null );
}else{
$post = array(
'post_title' => $_POST['title'],
'post_content' => $_POST['content'],
'post_status' => 'publish',
'tags_input' => $tags,
'post_type' => 'post'
);
wp_insert_post($post);
}
echo 'success';
exit;
Here I added 'tags_input' => $tags, to add a post tag.
Here is current situation:
1. No image selected: in the input field (a form with simple input field with title, description and tag), if I add something to the tag input while there is no image selected, then the post is created properly with the tag.
2. Image selected: if an image is selected, then with the tag input, it does not create the post at all.
I tried to add 'tags_input' => $tags, in the very first function, but that did not work either.
Can someone point out what change I need to make in my first function?
Suggest you add some var_dump statements if this is still a problem. When followed by an exit; statement, this will immediately show the data in the object.
I'd want to look at the state of the data within each if/then statement and the expected data in each function.
Of course, the data before and after your image is created/uploaded. Then I would expect the problem to become visible.
So generally, I'd do the following to debug the issue.
At various places put the following debugging code and you may also add the different critical variables using a comma separator in the var_dump function:
var_dump($_POST);
exit;
You also should consider using better variable names because the code
is more confusing to debug than it needs to be. The variable names are
not consistent even within a single if/then loop for example in the
second function you mention $data and then $post.

Multiple uri to same post in Wordpress

I wanted to make two uri to point same post in Wordpress but without redirect,
mysite.co.uk/awesome-post
mysite.co.uk/?p=12
I want those two uri to be pointed to same post but when you reach the page uri shouldn't change.
The only solution that i know is duplicate your post and get second link from it and hide the duplicate from loop and if you wish back end.
for that you have to use save_post action and plugin/theme activation hook for duplicate posts that have already published (this can be init hook but with caution you need to do this only one time).
first you have to loop through all posts and make duplicate
add_action('switch_theme', 'your_prefix_setup_options');
function your_prefix_setup_options () {
// WP_Query arguments
$args = array (
'post_type' => array( 'post' )
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// duplicate post like this
your_prefix_post_duplicate( get_the_ID(), get_the_title(), get_the_content(), get_post_thumbnail_id() );
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
}
function your_prefix_post_duplicate($post_id, $title, $content, $attachment){
$post_id = $post_id;
$post_name = $title;
$content = $content;
$attachment = $attachment;
$slug = str_replace( " ", "-", $post_name );
$slug = strtolower($slug);
$post_data = array(
'post_content' => $content,
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => 0,
'post_name' => $slug,
'post_title' => $post_name,
'post_status' => 'published',
'post_type' => 'post'
);
$prefix = 'your_prefix__';
//create your duplicate post
$duplicate_post_id = wp_insert_post( $post_data );
set_post_thumbnail( $duplicate_post_id, $attachment );
add_post_meta( $duplicate_post_id, $prefix . 'duplicate', TRUE );
//get duplicate link
$perma_link = get_permalink ( $duplicate_post_id );
//set this link to your post as meta
add_post_meta( $post_id, $prefix . 'duplicate_url', $perma_link );
}
// now you can get second uri for fb like/share
$second_link = get_post_meta(get_post_ID(),$prefix . 'duplicate_url', true);
//and you can use the meta and ignore duplicate post from showing on loop
// or in advance you can use pre get post to hidethese duplicates from front end & back end permanently
get_post_meta(get_post_ID(),$prefix . 'duplicate', true);

Wordpress and wp_insert_post not working

i'm doing an webapp that works with a wordpress website.
Until yesterday all works like a charm, but from today, when i save the post on wordpress db, it now not working...
Not give me error (php error or wordpress error), but simply a white page.
This is my full code:(sorry for many commented lines and echo, but i'm testing)
The important part:
// Create post object
$my_post = array(
'post_title' => $article_title,
'post_name' => $article_title,
'post_content' => $article_content,
'post_status' => 'pending',
'post_author' => 1
);
// Insert the post into the database
wp_insert_post($my_post, TRUE);// try first time
$post_status = "pending";
$post_type = "post";
$post_parent = "0";
$post_ID = (int) $wpdb->insert_id;
echo $post_ID; die;
Yesterday with a simple wp_insert_post(my_post); all worked fine, today (of course i think i edited some files...or i don't now), not works more and not give me error.
What do you think ?
Thank you, really!
EDIT: The problem was that i not insert post_category and wp need it!
You can capture the $post_id with the wp_insert_post() function...
$post_ID = wp_insert_post($my_post, TRUE);// try first time
Then you can dump out the $post_ID variable to get the ID or the error.
var_dump($post_ID);
add_action( 'init', 'my_func' );
function my_func() {
$my_post = '';
if( get_page_by_title($article_title,'OBJECT','post') == NULL )
$my_post= array(
'post_title' => $article_title,
'post_name' => $article_title,
'post_type' => 'post',
'post_content' => $article_content,
'post_status' => 'pending',
'post_author' => 1
);
$post_id = wp_insert_post( $my_post );
echo 'Post ID - '.$post_id;
}

Categories