最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Link to file in plugin directory from wordpress template?

programmeradmin4浏览0评论

I know that plugins and theme files should be kept separate but for for internal use I need to do it this way.

In my themes header.php file I want to include a php file which just contains html, from my plugin directory.

The path is basically /wp-content/plugins/my_plugin/my-html.php

I can't seem to figure out the proper code for wordpress to look in the plugins directory, and grab the my-html.php file from within the my_plugin folder. I want to include this file so the html in it is included in the header.php within my theme.

What would be the best way to go about this??

I know that plugins and theme files should be kept separate but for for internal use I need to do it this way.

In my themes header.php file I want to include a php file which just contains html, from my plugin directory.

The path is basically /wp-content/plugins/my_plugin/my-html.php

I can't seem to figure out the proper code for wordpress to look in the plugins directory, and grab the my-html.php file from within the my_plugin folder. I want to include this file so the html in it is included in the header.php within my theme.

What would be the best way to go about this??

Share Improve this question asked Oct 3, 2013 at 20:33 DamainmanDamainman 2531 gold badge6 silver badges17 bronze badges 1
  • I get what you're saying about needing to do it a certain way for internal use, I regularly encounter scenarios like this that are edge use-cases... ...but I think what you're basically looking at here is a template tag. I think that'd probably be the best way to do it. Let me look at my notes and see if I can provide an additional answer. – Tony Djukic Commented Apr 10, 2020 at 21:59
Add a comment  | 

5 Answers 5

Reset to default 3

In your main plugin file just define a constant containing the path of the plugin:

$pluginpath = plugin_dir_path( __FILE__ );
define('MY_AWESOME_PLUGIN_PATH', $pluginpath);

After that in your header.php:

include(MY_AWESOME_PLUGIN_PATH . 'html_file_name.html');

Check out WP's plugins_url function

<?php
 echo '<img src="' . plugins_url( 'images/wordpress.png' , __FILE__ ) . '" > ';
 ?>

check out more on the Wordpress Codex

If the plugins/my_plugin/my-html.php file is only going to output HTML, you could do it like this:

plugins/my_plugin/my-html.php

<?php
function my_output() {
    $html = 'This is some HTML that should go in the header.';
    echo( $html );
}

themes/my_theme/header.php

.
.
.
if( function_exists( 'my_output' ) ) {
    my_output();
}
.
.
.

You can include your files using

include '../plugins/my_plugin/plugin.php';

if the header of your plugin is in the right folder. .. goes back one folder to the wp-content, then u go to plugins/yourpluginfolder/phpfile.

As I stated in my comment I think what you're looking for, as it's basically just adding HTML from a plugin into a theme, is a template tag.

So in either your main plugin file, or a separate file you'll want to do something like this:

<?php
    // ADD TAG THAT CAN BE ADDED TO TEMPLATES
    function yourplugin_templatetag() {
        //you can echo out the lines of HTML here
        echo '<h1>This is a title.</h1>';
        echo '<p>This is the content of a paragraph.</p>';
        //Or you can close the PHP tags and just use regular HTML
        ?>
        <ul>
            <li>This is list item 1.</li>
            <li>This is list item 2.</li>
        </ul>
        <!--// Remember to re-open the PHP tags -->
    <?php }//yourplugin_templatetag()
?>

Then, in the template where you want this to show up, which is your header.php, you want to place the template tag we created for the plugin right where you want it's HTML to appear.

<?php yourplugin_templatetag(); ?>

If you made this a separate file you'll want to require the file in your main plugin file like so:

include( plugin_dir_path( __FILE__ ) . 'yourplugin_templatetag.php' );

Of course name the file what you like, as well as the template tag, and if the files in an includes directory or whatever, make sure you include that in the path.

发布评论

评论列表(0)

  1. 暂无评论