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 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵
  • ¥15 cfx离心泵非稳态计算