Plugin Development Output

时间:2016-07-19 作者:Ant Power

我正在尝试整合一个媒体上看到的onScrollBlur效果插件。

目前,我正试图通过一个短代码获得HTML的输出,但我什么也不输出,包括JS和CSS文件。

我不是PHP专家,只是想变得更好一点。

<?php
/**
* Plugin Name: OnScrollBlur
* Plugin URI: http://antpowerwebdevelopment.com
* Description: Medium/Twitter style Image Header onScrollBlur,great stylish header for your new build
* Version: 1.0.0
* Author: Ant Power
* Author URI: http://antpowerwebdevelopment.com
*
*/
?>
<?php

// register jquery and style on initialization
add_action(\'init\', \'register_script\');
function register_script(){
  wp_register_script( \'script_js\', plugins_url(\'/js/script.js\'));
  wp_register_style( \'style_css\', plugins_url(\'/css/style1.css\'));
}

// use the registered jquery and style above
add_action(\'wp_enqueue_scripts\', \'enqueue_style\');
function enqueue_style(){
  wp_enqueue_script(\'script_js\');
  wp_enqueue_style( \'style_css\' );
}

add_action( \'admin_menu\', \'register_onScrollPage\' );
function register_onScrollPage(){
  add_menu_page( \'onScrollBlur\', \'onScrollBlur\', \'manage_options\',   \'onScrollBlur_admin-menu\', \'onScollBlur_configure\', plugins_url( \'myplugin/images/icon.png\' ), 6 );
}

function onScrollBlur(){
  ?>
  <div class=\'blurImg\'>
    <div style="background-image: url(\'https://d262ilb51hltx0.cloudfront.net/fit/c/1600/1280/gradv/29/81/60/darken/25/1*4ncz3hLxmL8E_bUh-0z62w.jpeg\')"></div>
    <div class=\'blur\' style="background-image: url(\'https://d262ilb51hltx0.cloudfront.net/fit/c/1600/1280/gradv/29/81/40/darken/50/blur/50/1*4ncz3hLxmL8E_bUh-0z62w.jpeg\')"></div>
  </div>
  <header>
    <div>
      <h1>
        Medium
      </h1>
      <p>
        Everyone’s stories and ideas
      </p>
      <a href=\'https://medium.com/\' title=\'Medium\'>Learn more</a>
    </div>
  <?php
}

add_shortcode(\'onScrollBlur1\', \'onScrollBlur\');
?>

1 个回复
SO网友:ngearing

将脚本排入队列时,似乎缺少插件文件夹。plugins_url 将URL输出到您的wp plugins文件夹,它不包括您的自定义插件文件夹。

wp_register_script( \'script_js\', plugins_url(\'/YOUR_FOLDER/js/script.js\')); // Add your plugin folder
wp_register_script( \'script_js\', plugins_url(\'/js/script.js\',__FILE__)); // Let php add your folder
此外,您不需要先注册脚本/样式,只需将它们直接排入队列即可。只有当它们是其他脚本的依赖项时,首先注册它们才有用。

例如:。

add_action(\'wp_enqueue_scripts\', \'enqueue_style\');
function enqueue_style(){
  wp_enqueue_script( \'script_js\', plugins_url(\'/js/script.js\', __FILE__ ) );
  wp_enqueue_style( \'style_css\', plugins_url(\'/css/style1.css\', __FILE__ ) );
}
现在,对于您的短码输出,您需要使用php函数捕获输出ob_start()

function onScrollBlur(){
  ob_start();
  ?>
  <div class=\'blurImg\'>
    <div style="background-image: url(\'https://d262ilb51hltx0.cloudfront.net/fit/c/1600/1280/gradv/29/81/60/darken/25/1*4ncz3hLxmL8E_bUh-0z62w.jpeg\')"></div>
    <div class=\'blur\' style="background-image: url(\'https://d262ilb51hltx0.cloudfront.net/fit/c/1600/1280/gradv/29/81/40/darken/50/blur/50/1*4ncz3hLxmL8E_bUh-0z62w.jpeg\')"></div>
  </div>
  <header>
    <div>
      <h1>
        Medium
      </h1>
      <p>
        Everyone’s stories and ideas
      </p>
      <a href=\'https://medium.com/\' title=\'Medium\'>Learn more</a>
    </div>
  <?php
  return ob_get_clean();
}
您可以在此处阅读有关Shortcode API的更多信息:https://codex.wordpress.org/Shortcode_API#Output

相关推荐