wordpress - Insert posts programmatically while maintaining links - php

I am currently working on a migration script to insert articles from XML into Wordpress.
So far I parsed the XML and created arrays in PHP, I am looping through these arrays and insert them all one by one into Wordpress with the following code:
$post = array(
'post_title' => wp_strip_all_tags($article['title']),
'post_content' => $article['description'],
'post_status' => 'publish',
'post_author' => 1,
'ping_status' => 'closed',
'post_date' => $dateTime->format('Y-m-d H:i:s'),
'post_type' => $post_type
);
$result = wp_insert_post($post);
That all goes well, however here comes the issue: the XML's are an export from a website (unfortunately I do not know which CMS ) and in the content there can be links to files on the same site, for example:
<![CDATA[<p><strong>Shortcuts:</strong></p>
<p/>
<ul>
<li>(Booklet in Finnish)
</li>
<li>(Booklet in Swedish)
</li>
<li>(Booklet in Estonian)
</li>
<li>(Booklet in Russian)
</li>
</ul>]]>
Testsite.fi is my own site, so these are internal links.
Those links are referring to PDF's and this should be inserted into wordpress, but obviously the links will be different. I do have the PDF's that are being referred to ( for example: elakkeen_hakeminen_ulkomailta.pdf, and they are in same folder as this script is ) so all that is required is to upload this file in Wordpress programmatically or manually move it to the correct location, and then update the links so that it still works.
Any clue how to do this? I am guessing something with regular expressions, but can't really figure it out.

To change all internal links you can use this:
$content = preg_replace('%href="http://www\.testsite\.fi/(.*)"%', 'href="' get_bloginfo('wpurl') . '/$1"', $article['description'], -1);
$post = array(
'post_title' => wp_strip_all_tags($article['title']),
'post_content' => $content,
'post_status' => 'publish',
'post_author' => 1,
'ping_status' => 'closed',
'post_date' => $dateTime->format('Y-m-d H:i:s'),
'post_type' => $post_type
);
$result = wp_insert_post($post);
Since the pdfs in your example do not have a filetype they can't be identified programmatically. Otherwise it would be something along the lines of:
$upload_dir = wp_upload_dir();
$content = preg_replace('%href="http://www\.testsite\.fi/(.*)/(.*).pdf"%', 'href="' . $upload_dir['url'] . '/$2.pdf"', $article['description'], -1);
where $2 is the filename for the pdf.
Note:
The href part in the regex is not neccesary but assures that you are not changing urls that are not inside a href atrribute. Depending on the scenario you can leave that part out.

Related

Create a 2 different Posts in 2 different custom post type after function

I am trying to create a 2 different posts in 2 different post types. What i am trying to achieve is to have 2 unique posts, and if a button is clicked twice, it shouldn't create the post again.
<?
$custom_title = $company_name . '-' . $formindex;
$args = array(
'post_type' => 'dogovori',
'post_title' => $custom_title,
'post_name' => $company_name,
'post_status' => 'publish',
'post_content' => $content_dogovor,
);
$post_id = post_exists($custom_title) or wp_insert_post($args);
$args1 = array(
'post_type' => 'svidetelstva',
'post_title' => $custom_title,
'post_name' => $company_name,
'post_status' => 'publish',
'post_content' => $content_dogovor,
);
$post_id1 = post_exists($custom_title) or wp_insert_post($args1);
This is what i am using as a code. It works perfectly and creates the post only in 'dogovori'. What i want is to create at the same time in 'svidetelstva' as well.
The function post_exists does not pay any attention to your post type. So first you are inserting a new post with a custom title. When you check if a post exists the second time, it finds the first post you inserted.
Please reference this SA answer to check if a specific custom post exists with a title:
https://wordpress.stackexchange.com/a/300151

Is there a way to programmatically import wordpress pages within wordpress?

I'm trying to create this functionality within my wordpress plugin. Let's say I have a set number of pages that will never change and I want to automatically import them to every wordpress site I set up without having to manually go to the first site, export the xml file containing the pages then import it to the new site. Any thoughts on this?
Thanks
If you know how to loop through your XML file and your XML file is accessible on the other site you could loop through the following code:-
// Create post object
$my_post = array(
'post_title' => wp_strip_all_tags( $post_title ),
'post_content' => $post_content,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => $cat
);
// Insert the post into the database
wp_insert_post( $my_post );
You would need to initiate this code on the install of your plugin.
You can export your pages via default Wordpress export tool. That will result in .xml (WXR) file.
After that you can import the pages via WP-CLI tool on each one site, with the following command:
$ wp import file-name.xml
WXR stands for WordPress eXtended RSS.
You can store the pages in an array and then automatically insert them when your plugin is activated. I'd recommend storing a meta_key for each page that also let's you know it's already been inserted so that you don't create them every time the plugin is activated and deactivated. You can put this in the main file of your plugin. Be sure to replace the numbered pages and slugs with actual page names and to replace "my_plugin" with your plugin's namespace.
<?php
function create_my_plugin_pages() {
$pages = array(
'Page 1' => 'page-1', // Use slugs to create meta-keys
'Page 2' => 'page-2',
'Page 3' => 'page-3'
);
foreach( $pages as $title => $slug ) {
$meta_key = 'my-plugin_'.$slug;
// Check that the page wasn't already created
$existing = get_posts(array(
'post_type' => 'page',
'meta_query' => array(
array(
'key' => $meta_key,
'value' => '1'
)
)
));
// Create page if it doesn't exist
if ( !count($existing) ) {
$new_page = wp_insert_post(array(
'post_title' => $title,
'post_status' => 'publish'
));
add_post_meta($new_page,$meta_key,'1');
}
}
}
register_activation_hook( __FILE__, 'create_my_plugin_pages' );
?>

Inserting Wordpress Post via External Script

I'm trying to insert bulk posts via an external php script:
<?php
require_once '/full/path/to/wp-load.php';
require_once ABSPATH . '/wp-admin/includes/taxonomy.php';
$title = "some post title";
$content = "some content";
$tags= "tag1,tag2,tag3";
$user_id = 1;
// Create post object
$my_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_author' => $user_id,
'post_type' => 'post',
'tags_input' => $tags,
);
$id = wp_insert_post($my_post,true);
?>
$id returns 0 and WP_Error is empty.
Post is inserted to DB with right title and content but without tags. It also fails to use wp_insert_terms() to insert tags or other custom taxonomies.
Did i miss a file to include or is there something i didn't set right to work functions properly?
you don't need to load taxonomy.php.. this functionality is handled by wp-load.php
also i would suggest that you put this on first position in your file:
define( 'WP_USE_THEMES', false );
than you can use wp_insert_post() to add the post.. but to add the tags or category and so on you need to use
wp_set_post_terms()
or
wp_set_object_terms()
with your recieved $id from wp_insert_post.
for more informations check this: https://wordpress.stackexchange.com/questions/18236/attaching-taxonomy-data-to-post-with-wp-insert-post
Please include the wordpress database files first.
require_once 'wp-load.php';
require_once ABSPATH . '/wp-admin/includes/taxonomy.php';
and use the following code to insert post dynamically from the external script.
$newIds = wp_insert_post( array(
'post_title' => $postCSVContent['1'],
'post_content' => $postCSVContent['2'],
'post_type' => 'doors',
'post_status' => 'publish',
'post_author' => 1,
'post_parent' => $parentId
));
Refer the tutorial that explains how to create and update wordpress post programmatically.
http://www.pearlbells.co.uk/insert-udpate-wordpress-post-programmatically/

wp_post_insert HTML in post_content

I am trying to use wp_post_insert function to create posts/pages from my HTML pages.
So far I've got:
$html = file_get_contents($dir . $data['url']);
$post = array(
'post_content' => $html,
'post_name' => sanitize_title_with_dashes($data['title']),
'post_title' => $data['title'],
'post_status' => 'publish',
'post_type' => $data['type'],
);
$post_id = wp_insert_post($post);
However this does not seems to be working as post_content is always empty. I've tried different sanitation functions, but none of them are working.
HTML code is read into $html variable, but I guess the problem comes because its multiple lines.
Can you give me a hint?
Ive found that the files that I am reading were encoded with UCS2 encoding - file_get_contents returns special characters which looks good in browser (browser has its own character encoding), but not good for WP.
The solution was to reencode HTML files into UTF-8 ones.
Trick the function into thinking that the input has already been sanitized with the filter attribute:
$post = array(
'post_content' => $html,
'post_name' => sanitize_title_with_dashes($data['title']),
'post_title' => $data['title'],
'post_status' => 'publish',
'post_type' => $data['type'],
'filter' => true
);

WordPress wp_insert_post with HTML in post_content is adding \r\n

I'm creating a script outside of WordPress to insert new posts and WordPress is sanitizing my post in a weird way.
I will also be modifying the posts outside of WordPress.
I'm am using TinyMCE for the post_content textarea.
I'm using all WordPress functions by including:
include('../wp-config.php');
Here is my code that inserts my posts, but with \r\n for each new line:
$listing_phase = strtoupper($_POST['listing_phase']);
$listing_title = strtoupper($_POST['listing_title']);
$listing_description = $_POST['listing_description'];
$new_listing = array(
'post_title' => $listing_title,
'post_name' => str_replace('-', ' ', $listing_title),
'post_content' => $listing_description,
'tax_input' => array('property-status' => $listing_phase),
'post_status' => 'publish',
'post_type' => 'property',
'post_author' => 1
);
$listing_id = wp_insert_post($new_listing);
Here is the HTML that TinyMCE submitted:
<p>Studio</p>
<ul>
<li>Free Electric</li>
</ul>
This is what is in the database:
<p>Studio</p>\r\n<ul>\r\n<li>Free Electric</li>\r\n</ul>
When trying to modify a post I'm calling the_content(); from get_posts(); and \r\n are showing up.
How do I submit basic HTML content to WordPress's wp_insert_post function and get the content to view or modify later?
Thanks!
$listing_description = str_replace('\r\n','',$_POST['listing_description']);

Categories