duandian2725 2017-04-02 12:54
浏览 44

如何在wordpress短代码中转换这个PHP代码?

Hi I found this code on online, it is a simple button to reveal the coupon code and open the URL at the same time.

function coupon_add($coup, $uurl)
{
echo "    
<script type='text/javascript'>
<!--
function coupon(coup,url) 
{alert('COUPON CODE: ' + coup);
window.open(url,'_blank');
}
//-->
</script>";    
echo "<center><br /><input style=\"width:250px; height:60px;font-
size:30px;\" type=\"button\" onclick=\"coupon('".$coup."','".$uurl."')\" 
value=\"VIEW COUPON\" \/></center><p>";
}

I would like to turn this into a short code so I can insert it into a post. Would love some help with this, thanks in advance!

  • 写回答

1条回答 默认 最新

  • dongnuo2879 2017-04-02 13:04
    关注

    In your functions.php:

    add_shortcode( 'my_coupon_add', 'sc_my_coupon_add' );
    function sc_my_coupon_add( $args, $content = null ){
      $atts = shortcode_atts( array(
        'coup' => '',
        'uurl' => ''
      ), $args, 'my_coupon_add' );
      return "<script type='text/javascript'>
        function coupon( coup, url ){
          alert( 'COUPON CODE: ' + coup );
          window.open( url, '_blank' );
        }
       </script>  
       <center><br /><input style=\"width:250px; height:60px;font-size:30px;\" type=\"button\" onclick=\"coupon('" . $atts[ 'coup' ] . "','" . $atts[ 'uurl' ] . "')\" value=\"VIEW COUPON\" \/></center><p>";
    }
    

    Now you can use the shortcode [my_coupon_add coup="" uurl=""] and pass your parameters. The way the javascript is written, this can only be used once on a page or post or else you'll have issues with the coupon() function, but this should give you enough to scale/rewrite it if needed more than once (like moving the coupon() javascript function to a main js file so it's available once and only when needed).

    You might need to double-check the single and double quotes - I whipped this up quickly.

    评论

报告相同问题?

悬赏问题

  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒