0

I am planning to purchase a plugin that allows users to create polls and then embed them via shortcode into posts. It looks like a great plugin all around; however, the functionality I would like is not quite there. I would prefer it if the plugin would automatically add the post to the front end after the user clicks submit, instead of provide the user with a shortcode.

I would like to modify the code so that the process of creating a poll is as follows (italics is what I would need to implement):

  • User customizes his poll, clicks submit
  • The shortcode is generated, but the user does not see the shortcode
  • Some function creates a new post in WordPress, with the user who created the poll as the author, and the only content in that post is the shortcode that was just generated

Is there an easy way to do this? For context, I am a theme developer; however, I do not have any experience with developing plugins. I know a decent amount of PHP.

Could somebody please provide a generalized example of how I would go about doing this (i.e., getting the shortcode, then creating a post with the shortcode as the post's content)? Would I use wp_insert_post or something else?

Also, since this is another developer's plugin and I would be modifying the functionality of it, are there any best practices I should follow? For example, instead of overwriting their code, should I create a new plugin whose only purpose is to interact with the polling plugin and create posts out of its shortcodes?

  • You could just place the shortcode inside the new posts content? You don't need to process the shortcode and put its output in the post – Tom J Nowell Apr 16 '16 at 14:43
  • @TomJNowell Yes, that's true, but I want to make it as easy as possible for users to have their polls displayed on the site. I think some users may feel uncomfortable using shortcodes, and so I'd rather put in place an automatic shortcode generated -> post added process. – Michael Apr 16 '16 at 17:15
  • I mention it because in order to do what you ask, you'll need to peel away several layers of security, allowing arbitary html and javascript in your post content, the end result of which will be incredibly difficult to maintain. Once your users use these they can never use them again, and updates to the shortcode will be impossible as a result, and there is no realistic benefit to this system, as TinyMCE will mangle the markup, and the end result is the same. – Tom J Nowell Apr 16 '16 at 20:46
  • Or perhaps you've explained ambiguously and I've misunderstood. The literal interpretation of your question, to use youtube as an example, is to how to take a youtube embed, generate the raw HTML/javascript/iframe, and create a brand new post with that raw HTML/js/iframe. Perhaps if you could explain why you want to do this and what it's intended to achieve from a user perspective rather than a technical perspective, what would this system actually do? – Tom J Nowell Apr 16 '16 at 20:48
0

I played around with the plugin files and was able to figure it out myself. For anyone who's wondering, you can use wp_insert_post. You just need to find what variable the generated shortcode is stored in, which can take a little digging through the plugin files. For the plugin I was working with, it went something like this:

$my_poll = array(
    'post_content' => '[name_of_shortcode name_of_shortcode_attribute=' . $poll->_id . ']',
    'post_status' => 'publish'
    );

wp_insert_post($my_poll);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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