0

I am writing a plugin that enables subscribed users to submit posts of a custom type, and I am trying to implement this feature via shortcodes. Now the problem is that instead of inserting one post, my code inserts hundreds of them, until PHP runs out of memory... Here is an ultraminimal code that reproduces the issue:

add_shortcode( 'occaz-ajouter', 'occasion_addedit' );

function occasion_addedit(){

    wp_insert_post(array('post_title'=> 'hello', 
                        'post_content'=> 'Hello everybody',
                        'post_author'=>1));

    return "OK.";

}

(Of course, I have a page that contains the [occaz-ajouter] shortcode, and I encounter the problem when I visit this page.)

I saw that several people have encountered such infinite loops, but none of the solutions provided seems to solve my issue, since I use none of the hooks that usually explain the infinite loop... Any idea?

Thanks a lot,

Nown

3
  • Please add your solution as an answer, and mark your question as answered.
    – fuxia
    Commented Mar 18, 2013 at 10:00
  • I would love to, but I have to wait 8 hours since I have few reputation points...
    – Nown
    Commented Mar 18, 2013 at 10:12
  • Okay, please don't forget it. :)
    – fuxia
    Commented Mar 18, 2013 at 10:17

2 Answers 2

0

the problem lies with user capability when one use wp_insert_post to give other user the ability to insert post into WordPress. wp_insert_post uses the method current_user_can to check whether the given user has the capability to publish a post. After that i too have no idea what went wrong. However, this only happen when you set your post_status as publish. If you happen to change publish into other status such as draft, you will find that you can successfully insert your WordPress post without the problem of going into an infinity loop.

AND

But i want my user to have the capability of a subscribers but is able to publish a post themselves. If you are like me, you may want to try to bypass the checking on publish post. If you look into the code implementation of wp_insert_post, you will find that most of the checking are looking for the keyword publish. Another alternative to publish your post without using the keyword publish is to use alternative keyword, future.

http://hungred.com/how-to/wpinsertpost-infinity-loop-problem-return-blank-page/

Update

Weird! use remove_action( current_filter(), __FUNCTION__ ); before wp_insert_post and try.

4
  • Thank you very much for the answer. I just tried adding 'post_status'=> 'draft' and the same problem is still here (the hundreds of posts created now correctly appear as drafts)
    – Nown
    Commented Mar 18, 2013 at 8:58
  • @Nown please see the update.
    – revo
    Commented Mar 18, 2013 at 9:04
  • It does not seem to make any difference...
    – Nown
    Commented Mar 18, 2013 at 9:09
  • solution found (see update). Thanks for your time!
    – Nown
    Commented Mar 18, 2013 at 9:38
0

Ok, after digging for a while, I found the reason: the Relevanssi plugin expands every shortcode by default when building its index. For some reason, this caused the function referred to by my shortcode to be executed infinitely many times...

Thus the solution is simply to edit the Relevanssi settings to tell it not to expand shortcodes...

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.