web-dev-qa-db-fra.com

Développement de plugins multisites et wp_enqueue_script

J'ai un plugin que je développe. Cela fonctionne très bien sur une version à site unique de WordPress. Il installe une version multisite de WordPress, il semble fonctionner sur les pages d'administration, mais lorsque le plug-in tente d'ajouter du javascript et du css, il n'est pas ajouté.

La partie d'affichage du code est la suivante ...

   add_action('init', array(&$this, 'init'));

   function init() {
        // Gather gallery and template variables
        $url = explode('?', 'http://' . $_SERVER["HTTP_Host"] . $_SERVER["REQUEST_URI"]);
        $ID = url_to_postid($url[0]);
        load_plugin_textdomain('ngTemplateCreator', ngTemplateCreator_DIR . '/lang', basename(dirname(__FILE__)) . '/lang');
        $t = get_post($ID);
        $c = 0;
        $regex_pattern = get_shortcode_regex();
        preg_match_all('/' . $regex_pattern . '/s', $t->post_content, $regex_matches);

        // loop thru shortcode
        foreach ($regex_matches[3] as $key => $value) {
            if ($regex_matches[2][$key] == 'nggtc') {
                $c = $c + 1;
                //  Found a NextGEN gallery find out what ID
                //  Turn the attributes into a URL parm string
                $attribureStr = str_replace(" ", "&", trim($value));
                $attribureStr = str_replace('"', '', $attribureStr);

                //  Parse the attributes
                $defaults = array(
                    'id' => 0,
                    'template' => ''
                );
                $attributes = wp_parse_args($attribureStr, $defaults);

                if (isset($attributes["id"])) {
                    $this->gid = $attributes["id"];
                }
                if (isset($attributes["template"])) {
                    $this->tcname = $attributes["template"];
                }

                $wrapper = "nggtc_gallery_" . $this->gid . "_" . $c;

                if (!empty($this->gid) && !empty($this->tcname)) {
                    $images = get_ngg_gallery($this->gid);
                    $templates = get_custom_template($this->tcname);

                    if ($templates && $images) {
                        foreach ($templates as $template) {
                            if ($template->bottom_javascript) {
                                $this->bottom_javascript = $this->bottom_javascript . process_script_shortcode($template->bottom_javascript, $wrapper, $panelid, $panelcss, $filmstripid, $filmstripcss) . "\r\n\r\n";
                            }
                        }
                    }
                }
            }
        }
        $this->count = $c;
        if ($this->bottom_javascript) {
            add_action('wp_footer', array(&$this, 'footerScripts'), 200);
        }
        wp_reset_query();
    }

    function process_script_shortcode($script, $wrapper, $panelid, $panelcss, $filmstripid, $filmstripcss) {
        $out = remove_php($script);

        // Swap shortcode
        if($out) {
            // strip html from filmstrip variables
            $filmstripi = str_replace(" id=\"", "", $filmstripid);
            $filmstrip = str_replace("\"", "", $filmstripi);
            $filmstripcs = str_replace(" class=\"", "", $filmstripcss);
            $filmstripc = str_replace("\"", "", $filmstripcs);
            $out = str_replace("[wrapper]", $wrapper, $out);
            $out = str_replace("[panel_id]", $panelid, $out);
            $out = str_replace("[panel_css]", $panelcss, $out);
            $out = str_replace("[filmstrip_id]", $filmstrip, $out);
            $out = str_replace("[filmstrip_css]", $filmstripc, $out);
        }
        return $out;
    }

    function footerScripts() {
        if (wp_script_is('jquery', 'done')) {
            echo "<script type=\"text/javascript\" defer=\"defer\">" . $this->bottom_javascript . "</script>";
        }
    }

Je ne montre que la fonction de mes scripts de pied de page, mais aucun des scripts et css n'est affiché. Je suis donc presque certain que la source de l'erreur est la même.

Quelqu'un peut-il me dire où je me trompe ici?

2
Kevin Burnett

Comme @Even a commenté, au lieu de url_to_postid(), vous devriez utiliser quelque chose comme ceci:

if ( ! is_single() ) {
    return;
}

$post = get_post();

url_to_postid() est un moyen approximatif d'obtenir l'ID de la publication en cours, lorsque WordPress a déjà déterminé la nature de la publication en cours, c'est-à-dire qu'il a déjà analysé l'URL pour obtenir l'ID de la publication.

1
J.D.