Roots Discourse

Trying to get namespace for plugin class

#1

Hello, I am trying to get Multiple post Thumbnails to work, it requires I add this to functions.php, or now, extras.php, but not sure the namespace to give, since its in the plugins directory

if (class_exists('MultiPostThumbnails')) {
    new MultiPostThumbnails(
        array(
            'label' => 'Secondary Image',
            'id' => 'secondary-image',
            'post_type' => 'post'
        )
    );
}

this of course breaks the site. How do I fix?

0 Likes

#2

Give the namespaces section a read here: https://roots.io/upping-php-requirements-in-your-wordpress-themes-and-plugins/

But I would assume that since you are in a file with a namespace declared, you probably have to use the class_exists function like this: if (class_exists('\\MultiPostThumbnails')) {

Basically, it’s probably checking if the class exists in the current namespace, but adding \ to any class makes it look in the global namespace.

0 Likes

#3

I’ve studied up some more on it and things are working great! For example, I added this to extras

// limit excerpt by character count, not word count
function get_excerpt($count){
  $permalink = get_permalink($post->ID);
  $excerpt = get_the_content();
  $excerpt = strip_tags($excerpt);
  $excerpt = substr($excerpt, 0, $count);
  $excerpt = $excerpt.'';
  return $excerpt;
}

then on my index.php page, I used it like this:

at the top of the page, put: (to denote that I’m namespacing to extras:

<?php use Roots\Sage\Extras; ?>

then called my function:

<?= Extras\get_excerpt(122); ?>

and it worked! thanks!

0 Likes

#4

@mikeylythcott did you ever end up getting the Multiple Post Thumbnails to work? If so, how’d you do it?

0 Likes

#5

Nope, only if i put in in functions.php. Namespacing it as suggested by Kale Johnson killed the errors, but the plugin’s functionality still never actually displayed in the admin, unless I put it in functions.php

0 Likes