Support » Plugins » Hacks » Get theme from a sub directory only under themes folder

  • Hi! friends,

    I want to get all themes name which are inside a particular folder under wordpress default themes folder by wp_get_themes() function.But still unable to do that.It is very much important for my plugin which will choose a particular theme from that directory.

    Thanks in advance.

Viewing 12 replies - 1 through 12 (of 12 total)
  • Moderator bcworkz

    (@bcworkz)

    Why are you unable to use wp_get_themes()? It’s a good way to get all installed themes. If you want to go low level, php has several directory functions you could use, but you won’t know if the directory contains a valid theme.

    No no no wp_get_themes() returning all the themes from the the default theme directory. But I want from a particular directory only under the themes folder.
    e.g- I want themes name from wp-content/themes/tms not from wp-content/themes.

    Thanks.

    Moderator bcworkz

    (@bcworkz)

    You can pass the path to wp_get_theme(), which will return a WP_Theme object for that path, which contains the name and related info. As evidenced by this function returning a single object instead of an array, it appears a theme folder should only contain one theme.

    How would one place multiple themes in a folder? There can only be one style.css file.

    For better understanding I am providing the directory structure bellow –

    wp-content
    |-themes

    |–tms

    |—theme1

    |—theme2

    |—theme3

    I want to get the themes name form tms folder i.e. theme1,theme2,theme3 etc.

    Moderator bcworkz

    (@bcworkz)

    Sorry for my being so dense, I was unfamiliar with organizing themes that way. I took the time to investigate, it appears your best option is to go ahead and get all the themes with wp_get_themes(). Then step through the array returned, checking each WP_Theme object by getting the stylesheet path. In your example, the stylesheet paths of interest will all be tms/* . Obviously, you can then store the theme name or whatever related to each path matched for your plugin to use. Cheers.

    Thanks for suggestion. I will try that out. But can you tell me another thing is it possible to put another directory inside tms and get themes form there ?

    Thanks.

    Moderator bcworkz

    (@bcworkz)

    No, it appears only one level of sub-folders, the same as you already illustrated, is supported.

    Ok. I understand now. Thank you for your prompt response and suggestion.

    Thanks.

    Hi bcworkz,

    Once more ,can you tell me if it is possible to get themes name in random order by wp_get_themes or by some custom function in wordpress.

    Thanks.

    Moderator bcworkz

    (@bcworkz)

    😀
    No, but if you just want to pick a random theme do something like $result[array_rand($result)]; with your filtered by directory result taken from wp_get_themes().

    If you really need a full random array, I imagine there’s some simple algorithm someone’s already written you could use.

    bcworkz that is ok, but I want the themes name will be print in shuffle way means sequence will be shuffled. like- 1st > 4th > 2nd > 7th > 3rd > ….. etc.

    Thanks.

    Moderator bcworkz

    (@bcworkz)

    A very crude algorithm that should work OK would be, once you’ve determined the number of different themes, randomly generate theme index numbers and push them into a $random array only if that number does not already exist in the array, until it contains all the different, unique index numbers.

    In theory, there’s an infinitesimal chance that the loop could run longer than the allowable script time, thus the label of “crude”.

    Once the algorithm is done, you retrieve themes ‘randomly’ by stepping through the $random[] array in order, index 0-(#themes-1).

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Get theme from a sub directory only under themes folder’ is closed to new replies.