PHP - 如何使用其他目录文件中的函数?

时间:2015-01-30 16:07:12

标签: php wordpress

我在$ root / content / plugins / musicplayer / includes / player.php中有这个功能

public function head_script( $id, $playlist_id, $songs, $in_popup, $autoplay = false ) {
        $output   = '';
        $playlist = '';
        $artist   = '';
        $free     = null;
        $external = 0;

        if ( $songs ) {

            $ogg = '';

            foreach ( $songs as $song ) {

                $free = $song->free;

                if ( $song->poster ) {
                    $poster = esc_url( $song->poster );
                } else {
                    $poster = $this->get_default_playlist_poster( $playlist_id );
                }

                $playlist .= '{  title : "' . $song->name . '", mp3:"'. esc_url( $song->mp3 ) .'"';

                if ( $song->artist )
                    $playlist .= ', artist : "' . $song->artist . '" ';


                if ( $free != 'on' ) {

                $playlist .= ',poster : "' . $poster . '" ';


                $playlist .= ' },';
            }

            $playlist = substr( $playlist, 0, -1 );

            $output .= '<script type="text/javascript">//<![CDATA[';

            $output .= "\n";
            $output .= 'jQuery(document).ready(function($) {
                    new jPlayerPlaylist( {
                        jPlayer: "#jquery_jplayer_' . $id . '",
                        cssSelectorAncestor: "#jp_container_' . $id . '" }, 
                        ['.$playlist.'], {
                        swfPath: "' . WOLF_JPLAYER_PLUGIN_URL . '/assets/js/src",
                        wmode: "window", ';

            $output .= '});'; // end playlist

            if ( ! $in_popup )
                $output .= $this->popup();

            $output .= '});'; // end document ready playlist

            $output .= '//]]></script>';
        }

        echo $output;
    }

如何在$ root / content / themes / bigwolf / index.php中使用它,它仍然可以调用本机目录中最初和通常调用的所有函数,没有任何问题?

2 个答案:

答案 0 :(得分:1)

您可以加入它。这就是你如何做到的。

f1.php

<?php

    function func1()
    {
        echo 'hi';
    }

f2.php

<?php

    require_once('f1.php'); // require
    //include 'f1.php'; // or include

    func1();

答案 1 :(得分:0)

显然,musicplayer是一个插件。如果格式正确,WordPress会自动包含它。所以在你的插件的主文件中放

require_once __DIR__ . "/includes/player.php';

这会将你的player.php带入并让你访问其中的功能。

HTH,

= C =