简体   繁体   English

在钩子中运行shortcode联系人表单-Wordpress代码段

[英]Run shortcode contact form in a hook - wordpress code snippet

Ok this is my first ever attempt at PHP/code snippets so I apologies in advance if I'm way off the mark! 好的,这是我第一次尝试使用PHP /代码段,因此,如果我做不到,我会提前道歉!

I have a Wordpress website with a Real Estate Plugin that allows a logged in user to contact an agent (Landlord if you will). 我有一个带有房地产插件的Wordpress网站,该插件允许登录的用户联系代理(如果愿意,请联系房东)。 I would like to extend the plugin to use my own contact form (I'm using the plugin contact form 7) so that we get the email rather than the agent (We would then contact the agent on the users behalf) 我想扩展插件以使用我自己的联系表(我正在使用插件联系表7),以便我们获得电子邮件而非代理(然后我们将代表用户联系代理)

I found the contact agent hook below by searching through the plugin code 我通过搜索插件代码找到了下面的联系代理挂钩

      /**
     * single_property_contact_agent
     */
    public function single_property_contact_agent()
    {
        $property_form_sections = ere_get_option('property_form_sections', array('title_des', 'location', 'type', 'price', 'features', 'details', 'media', 'floors', 'agent'));
        $hide_contact_information_if_not_login = ere_get_option('hide_contact_information_if_not_login', 0);
        if ($hide_contact_information_if_not_login == 0) {
            if (in_array('contact', $property_form_sections)) {
                ere_get_template('single-property/contact-agent.php');
            }
        } else {
            if (is_user_logged_in()) {
                if (in_array('contact', $property_form_sections)) {
                    ere_get_template('single-property/contact-agent.php');
                }
            } else {
                ?>
                <p class="ere-account-sign-in"><?php esc_attr_e('Please login or register to view contact information for this agent/owner', 'essential-real-estate'); ?>
                    <button type="button" class="btn btn-primary btn-sm" data-toggle="modal"
                            data-target="#ere_signin_modal">
                        <?php esc_html_e('Login', 'essential-real-estate'); ?>
                    </button>
                </p>
                <?php
            }
        }

    }

My idea is to use code snippet below to hook onto the above and load our form instead (using the contact form shortcode) 我的想法是使用下面的代码片段连接到上面并加载我们的表单(使用联系表简码)

add_action('single_property_contact_agent', 'reg_form_before_content');

function reg_form_before_content() {

echo do_shortcode('[contact-form-7 id="524" title="Register Interest"]');

   }

Currently nothing happens so I maybe way off the mark of what is possible here but if anyone can help I would be forever grateful 目前什么都没有发生,所以我可能无法解决这里可能发生的事情,但是如果有人能帮助我,我将永远感激不已

Many thanks! 非常感谢!

You're on the mark, conceptually speaking. 从概念上讲,您很出色。 I think this is more or less The WordPress Way™: hooking into actions and filters in order to modify output/behaviour. 我认为这或多或少是WordPress Way™:连接到动作和过滤器以修改输出/行为。 So bravo to you there. 在那里真是太好了。

The trouble is, that function you're targeting doesn't implement any calls to apply_filter('filter_name', $data) , or do_action('hook_name', $var) . 麻烦的是,你的目标没有实现任何调用该函数 apply_filter('filter_name', $data) ,或do_action('hook_name', $var)

There's a tricky norm in a lot of WordPress code where the name of a function is also the name of a filter or action. 许多WordPress代码中都有一个棘手的规范,其中函数的名称也是过滤器或操作的名称。 But that is a coincidence. 但这是一个巧合。


All this being said, you might look at the source of the ere_get_template function. 说了这么多,您可以看一下ere_get_template函数的源代码。 Maybe it's employing a sort of template inheritance like WooCommerce does . 也许它像WooCommerce那样采用了某种模板继承

That is to say - if you create a template relative to your theme folder called single-property/contact-agent.php , it may check for a template you've provided, before defaulting to it's own. 也就是说-如果您相对于主题文件夹创建一个名为single-property/contact-agent.php的模板,则在默认为模板模板之前,它可能会检查您提供的模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM