开发 Wordpress-Plugin:将.js加载到 head 和 HTML 片段作为简码的问题



我被要求为我目前参与的一个项目开发一个WordPress插件,因为我通常做图形和UX设计(CSS3)。我对开发WP插件不是很熟悉,尽管我对PHP有相当的了解。我有以下代码:

<?php
/**
* Plugin Name: ExpButton Wordpress Plugin
* Plugin URI: https://url.de
* Description: Ein Generator des Expbuttons
* Version: 0.1
* Author: Wilko Meyer
* Author URI: http://url.com
**/
/**
* Loading js into header
**/
function add_async($url)
{
    if (strpos($url, '#asyncload')===false)
        return $url;
    else if (is_admin())
        return str_replace('#asyncload', '', $url);
    else
        return str_replace('#asyncload', '', $url)."' async='async";
}
add_filter('clean_url', 'add_async', 11, 1);

function expertbuttonjs()
{
    // Loading the JS
    wp_register_script( 'custom-script', plugins_url( 'https://www.expert-button.de/js.js#asyncload', __FILE__ ), array( 'jquery', 'jquery-ui-core' ), '20120208', true );
    wp_enqueue_script( 'custom-script' );
}
add_action( 'wp_enqueue_scripts', 'wptuts_scripts_with_the_lot' );
/**
*Creating Shortcode
**/
add_shortcode( 'shortcode', 'expbutton' );
function expbutton( $atts ) {
        /* Turn on buffering */
        ob_start(); ?>
        <div style="overflow:hidden;font-size:9px;height:auto;width:auto;text-align:center;margin:auto;" id="expertbuttonbg"><a target="_bl$
        <?php
    /* Get the buffered content into a var */
        $sc = ob_get_contents();
/**
*Expbutton to Shortcode
**/
add_shortcode( 'shortcode', 'expertbutton' );
function expertbutton( $atts ) {
        ob_start(); ?>
        <div style="overflow:hidden;font-size:9px;height:auto;width:auto;text-align:center;margin:auto;" id="expertbuttonbg"><a target="_bl$
        <?php
        $sc = ob_get_contents();
        ob_end_clean();
        /* Return the content as usual */
        return $sc;
}
?>

该插件的目的是将js.js作为异步加载到页面中,并从/* Turn on buffering */部分下方的HTML代码创建一个简码,该代码可以在wordpress网站上使用。

目前代码没有将js加载到标题中,并且应该创建的短代码也不起作用,我不知道为什么。希望有人可以帮助我解决这个问题并有一些线索。

为什么插件不起作用?

提前谢谢。

我做了一些重要的小改动。按钮的html代码不完整(所以我尝试猜测)。这个名字 ou 你的 javascript 文件在我看来非常奇怪和不寻常......

<?php
/**
* Plugin Name: ExpButton Wordpress Plugin
* Plugin URI: https://url.de
* Description: Ein Generator des Expbuttons
* Version: 0.1
* Author: Wilko Meyer
* Author URI: http://url.com
**/
/**
* Loading js into header
**/
function add_async($url)
{
    if ( strpos($url, '#asyncload') === false )
        return $url;
    else if ( is_admin() )
        return str_replace('#asyncload', '', $url);
    else
        return str_replace('#asyncload', '', $url)."' async='async";
}
add_filter('clean_url', 'add_async', 11, 1);

我已经更正wp_register_script()函数中的 url 和add_action()中也存在问题:您的 javascript 文件必须在插件根文件夹中(我的意思是不在子文件夹中)。另外,您的JS文件名在我看来很奇怪(通常它是以.js扩展名而不是.js#asyncload完成文件):

function expertbuttonjs()
{
    // Loading the JS 
    wp_register_script( 'custom-script', plugins_url('/js.js#asyncload', __FILE__ ), array( 'jquery', 'jquery-ui-core' ), '20120208', true );
    wp_enqueue_script( 'custom-script' );
}
add_action( 'wp_enqueue_scripts', 'expertbuttonjs' );

不要shortcode名称用于短代码函数。代码中的按钮代码不完整。此外,短代码或函数的名称不能有 2 倍相同:

/*
 * expbutton Shortcodes
 */
add_shortcode( 'expbutton', 'expbutton' );
function expbutton( $atts ) {
        /* Turn on buffering */
        ob_start(); ?>
        <div style="overflow:hidden;font-size:9px;height:auto;width:auto;text-align:center;margin:auto;" id="expertbuttonbg"><a target="_blank" href="'.$sc.'">Text Button</a><div>
        <?php
    /* Get the buffered content into a var */
        $sc = ob_get_contents();
/*
 *  expertbutton Shortcode
 */
add_shortcode( 'expertbutton', 'expertbutton' );
function expertbutton( $atts ) {
        ob_start(); ?>
        <div style="overflow:hidden;font-size:9px;height:auto;width:auto;text-align:center;margin:auto;" id="expertbuttonbg"><a target="_blank" href="'.$sc.'">Text Button</a><div>
        <?php
        $sc = ob_get_contents();
        ob_end_clean();
        /* Return the content as usual */
        return $sc;
}
?>

这现在应该有效。

最新更新