0

I have a very weird problem and I would really appreciate your help with it.

I am using wp_insert_post when a user submits a post. It works fine when I submit the form (i.e. the admin of the wordpress site), but when I try the same thing with a different user (i.e. different email, not the admin of the wordpress site), it only shows part of the content I used in the post_content field.

my code:

$my_post = array(
'post_title'    => 'My bet',
'post_content'  => '<form action="" method="post" id=form_id>' . 'bet ID: ' . '<input readonly id=betid     type=text value=' . $bet_id . '>' . '<input id=url name=urlpath type=hidden value=>' . '</form>' ."\r\n match: " . '<span id=match>' . $_POST[event] . '</span>' . "\r\n" . $current_user->user_login . "'s Pick: " . $_POST[bet] . '</span>' . '<span id=friends>' . '</span>' ."\r\n Amount: " . '<span id=amount>' . $_POST[amount] . '</span>' . "<?php include('wp-content/themes/twentyfourteen/test.php') ?>"
'post_status'   => 'publish');

When it is executed for the wordpress admin user (user ID=1) it works fine. when it is executed for any other user (user ID!=1) it is not showing the input fields of the form, and more important, not including the test.php I have included.

  • We'd need to see some more relevant code - what is wp-content/themes/twentyfourteen/test.php? – TheDeadMedic Mar 12 '14 at 11:51
  • it's just a php file that adds some elements to the page. There is no problem in that file. The problem is that the file itself is not included in the newly created post (I tried just putting there echo "hello" and it wont show up), and only when the wordpress user id is not 1 – user48840 Mar 12 '14 at 12:50
0

why don't you use a shortcode? add the shortcode in the content of that page and then create a function to show that form and include the php file.

the user with id 1 is always the super user/admin. I'm not sure but maybe there is a strict validation for other users when adding content to a post (this is just a guess). But using a shortcode should be no problem at all.

so you should end up with: (you should add an attribute for every variable, I don't added all of them i think)

$my_post = array(
'post_title'    => 'My bet',
'post_content'  => '[showform event="' . $_POST[event] . '" amount="' . $_POST[amount] . '" bet_id="' . $bet_id . '"][showform]',
'post_status'   => 'publish');

Somewhere else in your code you would add:

add_shortcode('showform', 'showForm');

function showForm() {
extract( shortcode_atts( array(
        'event' => 'default event',
        'amount' => 20,//default amount
             'bet_id' => 0
    ), $atts ) );
echo '<form action="" method="post" id=form_id>' . 'bet ID: ' . '<input readonly id=betid     type=text value=' . $bet_id . '>' . '<input id=url name=urlpath type=hidden value=>' . '</form>' ."\r\n match: " . '<span id=match>' . $event . '</span>' . "\r\n" . $current_user->user_login . "'s Pick: " . $_POST[bet] . '</span>' . '<span id=friends>' . '</span>' ."\r\n Amount: " . '<span id=amount>' . $amount . '</span>';

include('wp-content/themes/twentyfourteen/test.php');
}

if you added the shortcode function in the functions.php of your theme you should just do:

include('test.php');
  • thanks krike. I have tried what you said, but what is see on the post page is: [showform event="Real Madrid Vs. Chelsea" amount="9" bet_id="302"][showform] insted of the form itself. also, I dont understand how this will fix the problem that it only works for admin user and not for all users. – user48840 Mar 12 '14 at 16:34
  • If it is printing the shortcode itself instead of the form it means the code was not added properly. where did you place the second part of the code? The add_shortcode() ... ? – krike Mar 12 '14 at 18:17
  • I tried adding it before and after using the shortcut, in both cases it was the same. – user48840 Mar 13 '14 at 7:47
  • Place it somewhere in your functions.php of your theme at the top. make sure there is no if statement around it or anything. – krike Mar 13 '14 at 9:32
  • OK now it is showing, but the data that the form is showing is the default that was set, not the data that was submitted. any idea how to fix that? – user48840 Mar 13 '14 at 13:41

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.