• Resolved Paradise

    (@birobas)


    Estou criando uma forma de pessoas interagirem com o blog enviando um depoimento, daí estou criando um formulário no Front-end para permitir essa intera??o ent?o criei o seguinte script, porém ele n?o faz a postagem na página, poderia me ajudar?

    
    <?php
    
    function editor_randys_errors() {
      ?>
    <style>
    .editor-randys-error {border:1px solid #CC0000;border-radius:5px;background-color: #FFEBE8;margin: 0 0 16px 0px;padding: 12px;
    }
    </style>
      <?php
      global $error_array;
      foreach($error_array as $error){
        echo '<p class="editor-randys-error">' . $error . '</p>';
        }
    };
    
    function editor_randys_notices(){
      ?>
      <style>
      .editor-randys-notice {border:1px solid #E6DB55;border-radius:5px;background-color: #FFFBCC;margin: 0 0 16px 0px;padding: 12px;
      }
      </style>
      <?php
        global $notice_array;
        foreach($notice_array as $notice){
        echo '<p class="editor-randys-notice">' . $notice . '</p>';
        }
    };
    
    if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $POST['action'] ) && $_POST['action'] == 'post' ){
    
      $post_title = $_POST['post_title'];
      $post_content = $_POST['message'];
    
      if (empty($post_title)) $error_array[]='Por favor adicione um título.';
    // ??if (empty($post_content)) $error_array[]='Por favor adicione conteúdo.';
    
      $post = array( //our wp_insert_post args
    		'post_title'	=> $post_title,
    		'post_content'	=> $post_content,
        'post_status'	=> 'publish',
        'post_type' => 'testimonial'
    	);
    
    	$my_post_id = wp_insert_post($post); //send our post, save the resulting ID
    	
    }
    ?>
    
    <style>
    #box-post label{display:inline-block;width:15%;}
    #box-post input{width:60%;}
    #box-post input[type="submit"]{margin-left:15%;width:30%;padding:7px;}
    #box-post textarea{ display:inline-block;width:80%;vertical-align:top;}
    </style>
    
    <div class="post-box">
      <?php do_action( 'editor-randys-notice' ); ?>
      <form id="box-post" action="<?php the_permalink(); ?>" method="post">
        <p><label>Título: </label><input type="text" name="post_title"></p>
        <p><label>Mensagem: </label><textarea name="message" id="" tabindex="1" cols="30" rows="10"></textarea></p>
        <input type="submit" value="<?php esc_attr_e( 'Enviar Depoimento', 'editor-randys' ); ?>">
        <input type="hidden" name="action" value="post">
    
        <?php wp_nonce_field( 'new-post' ); ?>
      </form>
    </div>
    
    <?php
    
Viewing 6 replies - 1 through 6 (of 6 total)
  • I’m creating a way for people to interact with the blog by submitting a testimonial, so I’m creating a form in Front-end to allow this interaction so I created the following script, but it does not post to the page, could you help me?

    There’s a missing _ here:
    !empty( $POST['action'] )

    You are not checking the nonce.

    How is this code being called? (The do_action does not call it, unless that is in some code you didn’t show.)

    If you are trying to post them to that same page, it would be easier to just use comments.

    Thread Starter Paradise

    (@birobas)

    Olá, @joyously
    Eu adicionei o nonce, mas n?o funciona. Sim eu quero publicá-las na mesma página.
    Meu cliente n?o quer comentários, por isso estou tentando isso aí.

    do_action é apenas um hook que estou chamando minha fun??o editor-randys-notice.

    I added the nonce, but it does not work. Yes I want to post them to the same page. My client does not want comments, so I’m trying this out there. The_do_action is just a hook I’m calling my editor-randys-notice function.

    The nonce is for security, not for making it work. You also are not sanitizing the inputs, so I hope this will be added.

    Your code uses $post which is global, so I hope this doesn’t mess up the global.
    Did you add the missing _ so that the code in the if statement is executed?
    The do_action does not call your function. It will call any functions added to that action using add_action, which you didn’t in the code you shared. Perhaps this is for showing any existing testimonials?

    It looks like you don’t do anything with the ID of the new post. How do you know that the post is not being created? Did you look in the database with phpMyAdmin?

    Thread Starter Paradise

    (@birobas)

    Olá @joyously

    Eu já consegui resolver esse problema, agora estou com este problema AQUI! rs.

    Thread Starter Paradise

    (@birobas)

    Obrigado @joyously !!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Formulário no Front-end com wp_insert_post’ is closed to new replies.