WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#13354 closed defect (bug) (invalid)

wp_insert_post() not working correctly

Reported by: christian_gnoth Owned by:
Milestone: Priority: high
Severity: major Version: 3.0.1
Component: General Keywords: close 2nd-opinion
Focuses: Cc:

Description

Hi,

I tried to insert posts using the wp_insert_post function, but the post_content (with html) is truncated after 255 characters.

I inserted the posts per SQL INSERT statement native and it is working.

Change History (14)

comment:1 follow-up: lloydbudd4 years ago

  • Resolution set to invalid
  • Status changed from new to closed

Please try the support forums: http://wordpress.org/support/

comment:2 in reply to: ↑ 1 christian_gnoth4 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

Replying to lloydbudd:

Please try the support forums: http://wordpress.org/support/

If the SQL Insert is working correctly with the post_content the wp_insert_post should do the same.

comment:3 lloydbudd4 years ago

  • Resolution set to invalid
  • Status changed from reopened to closed

christian_gnoth, if you want to pursue this issue here, please provide a detailed report with example and code.

comment:4 nacin4 years ago

  • Milestone Unassigned deleted

comment:5 christian_gnoth4 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

hi,
here is an example:

 $query = "SELECT * FROM basic_pages ORDER BY page_id ";
  set_time_limit(25);
  $result = mysql_query($query, $CON);
  if ( !$result )
    echo mysql_error();
  
  $wp_pages = array();

  $vac_wp_tb_prefix = get_option('vac_wp_tb_prefix');

  $today = date("Y-m-d H:i:s");
  $insert_count = 0;
  while($R = mysql_fetch_array($result)) 
  {
    set_time_limit(25);

    $array = array(
        "post_author" => $user_id,
        "post_parent" => intval(0),
        "post_date" => $today,
        "post_date_gmt" => $today,
        "post_modified" => $today,
        "post_modified_gmt" => $today,
        "post_title"=>$R['page_title'],
        "post_status"=>"publish",
        "comment_status"=>"open",
        "ping_status"=>"open",
        "post_name" => sanitize_title($R['page_title']),
        "post_type" => "page"
      );

    $array["post_content"] = $R["page_text"]; 
    
    // Content Filter
    $array["post_content"] = str_replace('src="images/','src="/images/',$array["post_content"]);

wp_insert_post( $array );

  1. the function do not inserts the complete data. for example a string like:

"><a onclick="window.open('https://www.paypal.com/us/cgi-bin/webscr?cmd=xpt/Marketing/popup/OLCWhatIsPayPal-outside','olcwhatispaypal','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=400, height=350');" href="#">

is short cut to

"><a href="#">

it cuts off the string after 255 characters

comment:6 christian_gnoth4 years ago

any info to this problem???

comment:7 mfields4 years ago

  • Cc michael@… added
  • Keywords 2nd-opinion added; wp_insert_post post_content 255 characters removed
  • Resolution set to worksforme
  • Status changed from reopened to closed
  • Summary changed from wp_insert_pos not working correctly to wp_insert_post() not working correctly

I logged in a administrator and ran the following test to determine whether the post_content would be cut off at 255 characters. This did not happen. As for the removal of the onclick attribute, it was not removed via kses.

add_action( 'admin_head', 'create_gangsta_page' );
function create_gangsta_page() {
	wp_insert_post( array(
		"post_author" => 1,
		"post_title" => '1234 5678 9 This is just a test post',
		"post_status"=>"publish",
		"post_type" => "page",
		'post_content' => "<p>For sure <a onclick=\"window.open(' https://www.paypal.com/us/cgi-bin/webscr?cmd=xpt/Marketing/popup/OLCWhatIsPayPal-outside','olcwhatispaypal','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=400, height=350');\" href=\"#\">ipsizzle dolor</a> black amizzle, mah nizzle adipiscing elit. Fo shizzle go to hizzle velit, away volutpizzle, suscipizzle check out this, gravida vizzle, <a href='#' onclick='return false;'>arcu</a>. Dizzle cool tortizzle. Sed erizzle. Fusce at dolor dapibus turpis tempus check it out. Maurizzle pellentesque shut the shizzle up go to hizzle turpizzle. Fizzle check out this tortor. Pellentesque dope rizzle nisi. In hac habitasse for sure dictumst. The bizzle dapibus. Things tellizzle urna, pretium mammasay mammasa mamma oo sa, mattizzle ac, crazy pizzle, nunc. Bow wow wow suscipizzle. Integizzle semper velit <a href='http://google.com'>sizzle own yo'</a>. Donizzle yo mamma auctor maurizzle. Phasellus a elizzle ut mah nizzle gangsta tincidunt. Maecenas fo shizzle my shizz. Vestibulum izzle lacizzle doggy sure elementum break yo neck, yall. Nunc at dawg sizzle amet eros pimpin' porta. Mofo velizzle tortor, ultricizzle shiz, hendrerizzle quis, things, check it out. Etiam velit dope, aliquizzle consequat, tellivizzle ass, dictizzle fo shizzle mah nizzle fo rizzle, mah home g-dizzle, turpizzle. Its fo rizzle neque. Rizzle lorizzle. Phat vitae erat ut libero shizznit doggy. Fusce gangster augue eu nibh cool mattizzle. Phasellizzle fermentizzle sapien nizzle own yo'. Suspendisse lorem leo, sollicitudin sizzle, shizzle my nizzle crocodizzle izzle, commodo nec, justo. Donizzle i'm in the shizzle porttitor mah nizzle. Nunc feugizzle, crunk pot hizzle bow wow wow, sapizzle tellivizzle tincidunt pizzle, egizzle dapibus pede enim izzle izzle. Phasellizzle fo shizzle my nizzle leo, mah nizzle izzle, tempizzle izzle, semper izzle, crackalackin. Ut boom shackalack magna vizzle ipsum. Shizznit gizzle my shizz, phat vitae, vestibulum et, rutrizzle ma nizzle, velizzle. Maurizzle go to hizzle mauris. Sizzle sizzle magna sit dope risus iaculizzle fo shizzle my nizzle.</p>' END"
	) );
}

comment:8 christian_gnoth4 years ago

  • Version changed from 2.9.2 to 3.0.1

i tired this code:
<code>
add_action( 'admin_head', 'create_gangsta_page' );
$content = <<<EOD
"><a onclick="window.open(' https://www.paypal.com/us/cgi-bin/webscr?cmd=xpt/Marketing/popup/OLCWhatIsPayPal-outside','olcwhatispaypal','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=400, height=350');" href="#">
EOD;
$content = esc_attr( $content );
function create_gangsta_page() {

wp_insert_post( array(

"post_author" => 1,
"post_title" => '1234 5678 9 This is just a test post',
"post_status"=>"publish",
"post_type" => "page",
'post_content' => $content

) );

}
</code>

and the page was created, but without content.

comment:9 christian_gnoth4 years ago

  • Resolution worksforme deleted
  • Status changed from closed to reopened

comment:10 follow-up: christian_gnoth4 years ago

add_action( 'admin_head', 'create_gangsta_page' );
$content = <<<EOD
"><a onclick="window.open(' https://www.paypal.com/us/cgi-bin/webscr?cmd=xpt/Marketing/popup/OLCWhatIsPayPal-outside','olcwhatispaypal','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=400, height=350');" href="#">
EOD;
$content = esc_attr( $content );
function create_gangsta_page() {
	wp_insert_post( array(
		"post_author" => 1,
		"post_title" => '1234 5678 9 This is just a test post',
		"post_status"=>"publish",
		"post_type" => "page",
		'post_content' => $content
	) );
}

comment:11 in reply to: ↑ 10 duck_4 years ago

Replying to christian_gnoth:

add_action( 'admin_head', 'create_gangsta_page' );
$content = <<<EOD
<snip>

$content is not in the scope of create_gangsta_page so that's probably why it's not working. Either define $content in the function or use the global keyword.

comment:12 mfields4 years ago

  • Keywords close added
  • Resolution set to worksforme
  • Status changed from reopened to closed

I agree with duck_ and have verified that defining $content inside the function works. Resolving ticket to worksforme. Testing has shown that wp_insert_post() does not truncate post_content at 255 chars or remove link attributes if logged in as administrator.

comment:13 nacin4 years ago

  • Resolution worksforme deleted
  • Status changed from closed to reopened

comment:14 nacin4 years ago

  • Resolution set to invalid
  • Status changed from reopened to closed
Note: See TracTickets for help on using tickets.