実例1:SNSシェアボタン

<?php
/*
Plugin Name: My Simple SNS Share
Description: 投稿の最後にSNSシェアボタンを表示する
Version: 1.0
*/

function my_sns_share_add_buttons($content) {
    if (!is_single()) return $content;
    $url = urlencode(get_permalink());
    $title = urlencode(get_the_title());
    $buttons = '<div class="my-sns-share">';
    $buttons .= '<a href="https://twitter.com/intent/tweet?url=' . $url . '&text=' . $title . '" target="_blank">Xでシェア</a>';
    $buttons .= '</div>';
    return $content . $buttons;
}
add_filter('the_content', 'my_sns_share_add_buttons');

実例2:シンプルなお問い合わせフォーム

<?php
function my_contact_form_shortcode() {
    ob_start();
    if ($_SERVER['REQUEST_METHOD'] === 'POST' &&
        isset($_POST['my_contact_nonce']) &&
        wp_verify_nonce($_POST['my_contact_nonce'], 'my_contact_action')) {

        $name    = sanitize_text_field($_POST['name']);
        $email   = sanitize_email($_POST['email']);
        $message = sanitize_textarea_field($_POST['message']);

        wp_mail(get_option('admin_email'), 'お問い合わせ: ' . $name, $message);
        echo '<p>送信しました。</p>';
    }
    ?>
    <form method="post">
        <?php wp_nonce_field('my_contact_action', 'my_contact_nonce'); ?>
        <input type="text" name="name" required>
        <input type="email" name="email" required>
        <textarea name="message" required></textarea>
        <button type="submit">送信</button>
    </form>
    <?php
    return ob_get_clean();
}
add_shortcode('my_contact_form', 'my_contact_form_shortcode');
実運用にはさらなる対策が必要実際の公開プラグインでは、スパム対策(reCAPTCHA等)やメール送信の信頼性向上(SMTP経由送信)も必要です。詳しくはお問い合わせフォームの設置を参照してください。

関連コンテンツ

↑