2016-03-22 12 views
0

ich meine erste WP-Plugin gemacht, ist es ein Kontaktformular - hier ist der Code:Wordpress Shortcode für Kontaktformular Plugin nicht funktioniert

<?php 
/* 
Plugin Name: Example Contact Form Plugin 
Plugin URI: http://example.com 
Description: Simple non-bloated WordPress Contact Form 
Version: 1.0 
Author: Agbonghama Collins 
Author URI: http://w3guy.com 
*/ 

function html_form_code() { 
    echo '<form action="' . esc_url($_SERVER['REQUEST_URI']) . '" method="post">'; 
    echo '<p>'; 
    echo 'Your Name (required) <br />'; 
    echo '<input type="text" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="' . (isset($_POST["cf-name"]) ? esc_attr($_POST["cf-name"]) : '') . '" size="40" />'; 
    echo '</p>'; 
    echo '<p>'; 
    echo 'Your Email (required) <br />'; 
    echo '<input type="email" name="cf-email" value="' . (isset($_POST["cf-email"]) ? esc_attr($_POST["cf-email"]) : '') . '" size="40" />'; 
    echo '</p>'; 
    echo '<p>'; 
    echo 'Subject (required) <br />'; 
    echo '<input type="text" name="cf-subject" pattern="[a-zA-Z ]+" value="' . (isset($_POST["cf-subject"]) ? esc_attr($_POST["cf-subject"]) : '') . '" size="40" />'; 
    echo '</p>'; 
    echo '<p>'; 
    echo 'Your Message (required) <br />'; 
    echo '<textarea rows="10" cols="35" name="cf-message">' . (isset($_POST["cf-message"]) ? esc_attr($_POST["cf-message"]) : '') . '</textarea>'; 
    echo '</p>'; 
    echo '<p><input type="submit" name="cf-submitted" value="Send"/></p>'; 
    echo '</form>'; 
} 

function deliver_mail() { 

    // if the submit button is clicked, send the email 
    if (isset($_POST['cf-submitted'])) { 

     // sanitize form values 
     $name = sanitize_text_field($_POST["cf-name"]); 
     $email = sanitize_email($_POST["cf-email"]); 
     $subject = sanitize_text_field($_POST["cf-subject"]); 
     $message = esc_textarea($_POST["cf-message"]); 

     // get the blog administrator's email address 
     $to = get_option('admin_email'); 

     $headers = "From: $name <$email>" . "\r\n"; 

     // If email has been process for sending, display a success message 
     if (wp_mail($to, $subject, $message, $headers)) { 
      echo '<div>'; 
      echo '<p>Thanks for contacting me, expect a response soon.</p>'; 
      echo '</div>'; 
     } else { 
      echo 'An unexpected error occurred'; 
     } 
    } 
} 

function cf_shortcode() { 
    ob_start(); 
    deliver_mail(); 
    html_form_code(); 

    return ob_get_clean(); 
} 

add_shortcode('contact_form', 'cf_shortcode'); 
?> 

This ist das Tutorial ich es früher.

Wenn ich [contact_form] in einem Beitrag oder auf einer Seite setzen, erscheint nichts ... kein Kontaktformular.

Warum wird das Formular nicht angezeigt?

Antwort

1

Aktivieren Sie das Plugin im Administrationsbereich.

+0

haha, danke für den Vorschlag, aber das ist nicht das Problem, ich stellte sicher, dass es aktiviert ist ... und jedes Mal, wenn ich eine Änderung an den Code ich deaktivieren und reaktivieren. – ewizard

+0

@ewizard Ich habe Ihren Code kopiert und es funktioniert gut. Testen Sie es auf einem sauberen und aktualisierten Wordpress? Ich schlage vor, dass Sie versuchen, alle anderen Plugins zu deaktivieren, um sicherzustellen, dass es keinen Konflikt gibt. –

+0

Ich versuchte es mit allen Plugins deaktiviert ... Ich arbeite mit der neuesten Version oder Wordpress, obwohl ich nicht sicher bin, ob es "sauber" ist oder nicht - ich fand auch eine Abhilfe, die ich als Antwort posten werde ... ich denke, es ist eine ziemlich häufige Arbeit für Shortcode-Fehler. – ewizard

0

Anstatt [contact_form] im Wordpress-Editor zu verwenden, habe ich <?php echo do_shortcode('[contact_form]'); ?> in der PHP-Vorlage für diese Seite verwendet.

Verwandte Themen