首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WordPress CF7添加

WordPress CF7添加
EN

Stack Overflow用户
提问于 2018-03-24 13:08:50
回答 1查看 158关注 0票数 0

对于即将到来的GDPR,我正在创建一个插件,为我的客户的每个站点添加一些片段。

我现在遇到问题的代码,应该检查是否安装了联系人表单7,以及是否存在带有post类型'wpcf7_contact_form‘和标题'Kontakt’的帖子。如果是的话,它应该获取post内容,并检查是否存在提交和验收字段。如果有提交按钮,但没有接受字段,则应该将接受字符串(包括按钮短代码)与找到的提交部分连接起来,并在post内容中替换它。

经过一些测试后,我注意到检查是否存在post,以及是否安装了cf7。其余的都不是用http://www.phpliveregex.com/创建的http://www.phpliveregex.com/模式。我现在不知道为什么不是woking,谢谢你提前提供帮助。

编辑:经过一些调试(还在代码中),我注意到现在check_submit和check_acceptance函数不能工作了。其余的coe现在应该运行。

守则:

代码语言:javascript
复制
<?php 

add_action( 'admin_init', 'init_cf7_privacy');
function init_cf7_privacy() {

    if ((check_cf7_installation()) && (find_contact_form() != "")) {

        $formContent = get_contactform_content();
        $acceptance = '[acceptance acceptance-842]<small>Ich habe die <a href="/datenschutzerklaerung">Datenschutzerklärung</a> zur Kenntnis genommen. Ich stimme zu, dass meine Angaben zur Kontaktaufnahme und für Rückfragen dauerhaft gespeichert werden. </small>[/acceptance]';

        if (check_cf7_installation()) 
            echo "CF7 Returned true!" . "\r\n";

        echo "CF Page ID: " . find_contact_form();

        if (check_submit( $formContent )) 
            echo "Submit Returned true!" . "\r\n";

        if (check_acceptance( $formContent )) 
            echo "Acceptance Returned true!" . "\r\n";

        //echo concatenate_acceptance_submit( $formContent, $acceptance ) . "\r\n";
        echo add_acceptance( $formContent, $acceptance );

        $my_post = array(
              'ID'           => find_contact_form(),
              'post_content' => add_acceptance( $formContent, $acceptance ) ,
        );

        // Update the post into the database
        wp_update_post( $my_post ); 

    }
}

function check_cf7_installation() {
    if (class_exists('wpcf7'))
        return true;
}

function find_contact_form() {
    $searchTitle = 'Kontakt';
    $page = get_page_by_title( $searchTitle, OBJECT, 'wpcf7_contact_form');

    return $page->ID;
}

function get_contactform_content() {
    $my_postid = find_contact_form();
    $post_object = get_post($my_postid);
    $content = $post_object->post_content;

    return $content;
}

function concatenate_acceptance_submit( $formContent, $acceptance ) {
    if( preg_match('/.+\[submit\s["].+["]\].+/', $formContent, $matches ) ) {
        return $acceptance . "\r\n" . "\r\n" . $matches[0];
    }
}

function check_submit( $formContent ) {
    if ( preg_match('/\[submit\s["].+["]\]/', $formContent) )
         return true;
}

function check_acceptance( $formContent ) {
    if ( preg_match('/\[acceptance\s.+\]/', $formContent) )
         return true;
}

function add_acceptance( $formContent, $acceptance ) {
    if ( check_submit( $formContent ) && !check_acceptance( $formContent ) ) {

        if ( preg_match('/.+\[submit\s["].+["]\].+/', $formContent, $matches) ) {

            $formContentRep = str_replace( $matches[0], concatenate_acceptance_submit( $formContent, $acceptance ), $formContent );

            return $formContentRep;

        }
    }
}

成功接收到的preg_match功能的post内容和主题:

代码语言:javascript
复制
<p>Ihr Name (Pflichtfeld)<br />    [text* your-name] </p>
<p>Ihre E-Mail-Adresse (Pflichtfeld)<br />
[email* your-email] </p>

<p>Betreff<br />
[text your-subject] </p>

<p>Ihre Nachricht<br />
[textarea your-message x3] </p>


<p class="submit">[submit "tohuwabohu"]</p>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-28 00:53:45

我不知道确切原因,但是wp_update_post()使chek函数不能正常工作。下面的wpdb查询代码正在运行:

代码语言:javascript
复制
<?php 

add_action( 'admin_init', 'init_cf7_privacy');
function init_cf7_privacy() {

    if ((check_cf7_installation()) && (find_contact_form() != "")) {

        $formContent = get_contactform_content();
        $acceptance = '[acceptance acceptance-842]<small>Ich habe die <a href="/datenschutzerklaerung">Datenschutzerklärung</a> zur Kenntnis genommen. Ich stimme zu, dass meine Angaben zur Kontaktaufnahme und für Rückfragen dauerhaft gespeichert werden. </small>[/acceptance]';

        /*
        if (check_cf7_installation()) 
            echo "CF7 Returned true!" . "\r\n";

        echo "CF Page ID: " . find_contact_form() . "\r\n";

        if (check_submit( $formContent )) 
            echo "Submit Returned true!" . "\r\n";

        if (check_acceptance( $formContent )) 
            echo "Acceptance Returned true!" . "\r\n";

        echo concatenate_acceptance_submit( $formContent, $acceptance ) . "\r\n";
        */

        add_acceptance( $formContent, $acceptance );

    }
}

function check_cf7_installation() {
    if (class_exists('wpcf7'))
        return true;
}

function find_contact_form() {
    $searchTitle = 'Kontakt';
    $page = get_page_by_title( $searchTitle, OBJECT, 'wpcf7_contact_form');

    return $page->ID;
}

function get_contactform_content() {
    $my_postid = find_contact_form();

    global $wpdb;
    $content = $wpdb->get_var( 
        $wpdb->prepare( "
            SELECT meta_value
            FROM ".$wpdb->prefix."postmeta
            WHERE post_id = %d
            AND meta_key = %s
            ",
           $my_postid, '_form' ) 
    );

    return $content;
}

function concatenate_acceptance_submit( $formContent, $acceptance ) {
    if( preg_match('/.+\[submit\s["].+["]\].+/', $formContent, $matches ) ) {
        return $acceptance . "\r\n" . "\r\n" . $matches[0];
    }
}

function check_submit( $formContent ) {
    if ( preg_match('/\[submit\s["].+["]\]/', $formContent) )
        return true;
}

function check_acceptance( $formContent ) {
    if ( preg_match('/\[acceptance\s.+\]/', $formContent, $matches) )
        return true;
}

function add_acceptance( $formContent, $acceptance ) {
    if ( check_submit( $formContent ) && !check_acceptance( $formContent ) ) {

        if ( preg_match('/.+\[submit\s["].+["]\].+/', $formContent, $matches) ) {

            $formContentRep = str_replace( $matches[0], concatenate_acceptance_submit( $formContent, $acceptance ), $formContent );

            //Write new Cotact Form Content to Database
            global $wpdb;    

            $wpdb->query(
                $wpdb->prepare( "
                    UPDATE ".$wpdb->prefix."postmeta
                    SET meta_value = %s
                    WHERE post_id = %d
                    AND meta_key = %s
                    ", 
                    $formContentRep, find_contact_form(), '_form' )
            );

        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49465043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档