dongxiaowei_1234 2018-04-17 09:00 采纳率: 0%
浏览 84
已采纳

在自定义WordPress插件中加载图像

I created my custom plugin and want to add custom logo, but somehow it keeps redirecting me to wrong dir, this is error I get:

GET http://localhost/logo.png 404 (Not Found)

Here is what I tried so far:

$plugin_dir = str_replace( $base_url, ABSPATH, $plugins_url );
$plugin_dir = plugins_url();
$plugin_dir = plugin_dir_path( __FILE__ );
$plugin_dir = WP_CONTENT_DIR . '/plugins';
$plugin_dir = plugins_url( '/', __FILE__ );

Here is my add action:

    function kalbos_modifymenu() {

        add_menu_page('Kalbos', //page title
            'Kalbos', //menu title
            'manage_options', //capabilities
            'kalbos_list', //menu slug
            'kalbos_list', //function
            $plugin_dir . 'logo.png',
            '5'
            );
}
    add_action('admin_menu','kalbos_modifymenu');

However icon is working when i move my logo to wp-admin/logo.png dir because thats where my logo path goes even if i set it go to my plugin folder

  • 写回答

2条回答 默认 最新

  • dongyou2279 2018-04-17 14:15
    关注

    plugins_url will (my emphasis):

    Retrieves the absolute URL to the plugins or mu-plugins directory (without the trailing slash) or, when using the $path argument, to a specific file under that directory. You can either specify the $path argument as a hardcoded path relative to the plugins or mu-plugins directory, or conveniently pass __FILE__ as the second argument to make the $path relative to the parent directory of the current PHP script file.

    So, the following will point exactly to your plugin directory with an ending slash:

    $plugin_dir = plugins_url( '/', __FILE__ );
    // results in http://localhost/wp-content/plugins/YOUR_PLUGIN/
    

    And to target the logo, use:

    $plugin_dir . 'logo.png'
    

    Another example, to make a shortcut to your images folder:

    $plugin_img = plugins_url( '/images/', __FILE__ );
    // results in http://localhost/wp-content/plugins/YOUR_PLUGIN/images/
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?