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 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式