【问题标题】:Adding MailChimp Shortcode in Theme with PHP使用 PHP 在主题中添加 MailChimp 短代码
【发布时间】:2018-02-01 22:28:12
【问题描述】:

我有以下网站:https://www.ajagrawal.com

目前在我的网站上,我正在尝试编辑英雄部分,并将名称、电子邮件和按钮字段替换为使用插件制作的自定义 wordpress 表单。现在电子邮件存储在 Wordpress 中,但我希望它们在 Mailchimp 中。

我知道我的主题中的哪些代码属于以下形式:

if(function_exists('newsletter_form')):
          $output .=  '<div class="row">';
          $output .=  '<div class="col-sm-6 col-sm-offset-6 col-md-4 col-md-offset-6">';
          $output .=  '<form method="post" action="'.home_url('/').'?na=s" onsubmit="return newsletter_check(this)">';
          $output .=  '<div class="c-input-3-wrapper">';
          $output .=  '<input class="c-input type-3" type="text" name="nn" required="" placeholder="'.esc_html($name_placehodler).'">';
          $output .=  '<div class="c-input-3-icon"><span class="lnr lnr-user"></span></div>';
          $output .=  '</div>';
          $output .=  '<div class="c-input-3-wrapper">';
          $output .=  '<input class="c-input type-3" type="email" name="ne" required="" placeholder="'.esc_html($email_placeholder).'">';
          $output .=  '<div class="c-input-3-icon"><span class="lnr lnr-envelope"></span></div>';
          $output .=  '</div>';
          $output .=  '<input class="newsletter-submit c-btn type-1 size-4 full" type="submit" value="'.esc_html($btn_text).'">';
          $output .=  '</form>';
          $output .=  '</div>';
          $output .=  '</div>';
        endif;

这是我的代表表单的 mailchimp 短代码。

[mc4wp_form id="2198"]

我尝试过编辑代码并以多种方式插入它,甚至最终导致网站崩溃,但仍然没有运气。考虑到我的目标,这是正确的方法(将短代码放在 PHP 代码中)吗?

如果是这样,如何做到这一点?

【问题讨论】:

    标签: php wordpress wordpress-theming mailchimp


    【解决方案1】:

    您可以使用do_shortcode 函数。

    documentation 中的示例:

    // Use shortcode in a PHP file (outside the post editor).
    echo do_shortcode( '' );
    
    
    // In case there is opening and closing shortcode.
    echo do_shortcode( '[iscorrect]' . $text_to_be_wrapped_in_shortcode . '[/iscorrect]' );
    
    
    // Enable the use of shortcodes in text widgets.
    add_filter( 'widget_text', 'do_shortcode' );
    
    // Use shortcodes in form like Landing Page Template.
    echo do_shortcode( '[contact-form-7 id="91" title="quote"]' );
    
    // Store the short code in a variable.
    $var = do_shortcode( '' );
    echo $var;
    

    所以在你的情况下是这样的:

    echo do_shortcode( '[mc4wp_form id="2198"]' );
    

    应该可以解决问题。

    编辑: 像这样的东西应该可以工作:

              $output .=  '<div class="row">';
              $output .=  '<div class="col-sm-6 col-sm-offset-6 col-md-4 col-md-offset-6">';
              $output .=  do_shortcode( '[mc4wp_form id="2198"]' );
              $output .=  '</div>';
              $output .=  '</div>';
    

    然后对输出做任何你想做的事情 - 可能必须回显它......

    【讨论】:

    • 我是否会删除整个 if 语句并包括 echo do_shortcode( '[mc4wp_form id="2198"]' );还是我会删除它的某些行?
    • 根据我从您的代码中可以理解的内容,我认为您需要更改 if 语句以检查 id 为 2198 的表单是否存在,如果存在则添加布局 - 行,col-* divs 然后是短代码回显(基本上代码中的表单被回显短代码和 if 语句更改)如果不清楚,请告诉我
    • 我目前正在熟悉 PHP。逻辑是有道理的,但是如何准确地“添加布局 - 行,col-* divs”,然后 echo do_shortcode( '[mc4wp_form id="2198"]' );我很困惑。
    • 嗨 Jantea,有什么要澄清的吗?
    • @RohitTigga:对不起,昨天错过了你的评论,我已经用你应该可以使用的代码更新了我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    相关资源
    最近更新 更多