在wordpress中联系表单7如何在正文中包含javascript src [英] contact form 7 in wordpress how to include javascript src in body

查看:82
本文介绍了在wordpress中联系表单7如何在正文中包含javascript src的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用wordpress,并联系表单7插件。
我想要完成的是,一些下拉菜单中填充了自定义javascript,我知道你可以将它包含在另一个插件Scripts and Stuff中。但我想把它放在一个单独的文件中,并将文件包含在联系表单的body标签中,并附带脚本src =http://example.org/myscript.js(使用插件创建的实际表单)

I'm using wordpress, and contact form 7 plugin. What's i'm trying to accomplish is that some of the drop down menus are populated with custom javascript, I know that you can include it with another plugin "Scripts and Stuff" for example. but I wanted to put it in a seperate file and include the file in the body tags of the contact form with script src="http://example.org/myscript.js" (actual form created with the plugin)

编辑:我不想将它添加到主题的footer.php,我希望脚本src只在单页上。不是整个主题

i donot want to add it to a footer.php of the theme, I want the script src to be on a Single page only. not the entire theme

推荐答案

当然,这很直截了当。有两种方法可以实现这一点,

sure, that's quite straight forward. There are 2 ways to achieve this,


  1. 你可以用你的js代码<<插入< script> / script> 位于cf7表单编辑器内部表单的底部,这样只有在页面上打印表单时才会加载脚本。但是,请确保您不在js代码中添加任何新行空格,否则cf7插件将插入空< p>< / p> 元素,这将会破坏执行时的代码。或者,

  2. 如果您正在运行WP4.7,现在可以使用 do_shortcode_tag 实际上将您的脚本排入与打印表单相同的页面上,但是记得先注册那个脚本,

  1. you can either insert a <script>with your js code</script> at the bottom of your form inside the cf7 form editor, this way the script loads only when the form is printed on the page. However, make sure you don't add any new lines spaces in your js code else cf7 plugin will insert empty <p></p> elements and this will break your code at execution. Alternatively,
  2. if you are running WP4.7 you can now use the do_shortcode_tag which has been introduced with this release to actually enqueue your script on the same page as which the form is being printed, but remember to register that script earlier on first,

add_filter('wp_enqueue_scripts', 'register_my_script');
function register_my_script(){
  wp_register_script( 'my-script', get_stylesheet_directory_uri() . 'js/my-script.js', array( 'jquery' ), "1.0" , false );
}
add_filter('do_shortcode_tag', 'enqueue_my_script',10,3);
function enqueue_my_script($output, $tag, $attr){
  if('contact-form-7' != $tag){ 
    return $output; //make sure we filter cf7 shortcodes
  }
  if(isset($attr['id']) && '4' == $attr['id']){
    wp_enqueue_script('my-script'); //enqueue if it is form id=4
  }
  return $output;
}


这篇关于在wordpress中联系表单7如何在正文中包含javascript src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆