简体   繁体   中英

How to insert full content of regex data into wordpress database via PHP?

I'm trying to insert new post to WORDPRESS using PHP. So this is my code.

I'm using regex to get the data I want and insert it into the database when I run it without insert into the database, its working fine. But whenI try to insert, my code inserts only the last image files...It's must be "x" file.

<?
$link = 'http://example.com/index.html';
$get = file_get_contents($link);
//get title
if (preg_match_all('/title="RSS 2.0" href="...........................(.*).html/',$get,$title))
foreach($title[1] as $orgtitle)
{
    $title = str_replace("-"," Vol.","$orgtitle");
    echo $title."\n";
}
//get content
if (preg_match_all('/<span class = "photoThum" ><a href="(.*?)"/',$get,$description))
foreach($description[1] as $content)
{
    $content = str_replace("http://","https://","$content");
    $content2 = '<img src= "'.$content.'" />'."\r\n";
    echo $content2;
}
//mysql connect
$servername = "localhost";
$username = "root";
$password = "test";
$dbname = "test";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO wp_posts (post_title, post_content, post_status, post_excerpt, to_ping, pinged, post_content_filtered)
VALUES ('$title', '$content2', 'draft', '', '', '', '')";

if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>

Am i doing wrong anything ?

wp-load.php is responsible for bootstrapping the WordPress environment which makes the plugin able to use the native WordPress Core function.

<?php
include 'wp-load.php';

$link = 'http://example.com/index.html';
$get = file_get_contents($link);

//get title
if (preg_match_all('/title="RSS 2.0" href="...........................(.*).html/',$get,$title))
foreach($title[1] as $orgtitle){
    $title = str_replace("-"," Vol.","$orgtitle"); 
}
//get content
if (preg_match_all('/<span class = "photoThum" ><a href="(.*?)"/',$get,$description))


foreach($description[1] as $content){
    $content = str_replace("http://","https://","$content");

    $content2 = '<img src= "'.$content.'" />'."\r\n";

    $post_id = wp_insert_post(
        array(
            'comment_status'    =>      'closed',
            'ping_status'       =>      'closed',
            'post_title'        =>      $title,     // Enter you title
            'post_content'      =>      $content2,
            'post_status'       =>      'publish',
            'post_type'         =>      'YOUR_POST_TYPE_NAME'   // Enter your posttype name
        )
    );

    echo "post_id :".$post_id."<br>";
}
?>

Have you tried wp_insert_post() , it will handle the prepared statement, validation and sanitization for you. If you are working outside WordPress you can easily load the WordPress core by requiring wp-load.php , and then insert posts using wp_insert_post() , more about in this post .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM