WordPress.org

Ready to get started?Download WordPress

Forums

Get theme from a sub directory only under themes folder (13 posts)

  1. s_ghosh
    Member
    Posted 1 year ago #

    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.

  2. bcworkz
    Member
    Posted 1 year ago #

    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.

  3. s_ghosh
    Member
    Posted 1 year ago #

    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.

  4. bcworkz
    Member
    Posted 1 year ago #

    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.

  5. s_ghosh
    Member
    Posted 1 year ago #

    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.

  6. bcworkz
    Member
    Posted 1 year ago #

    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.

  7. s_ghosh
    Member
    Posted 1 year ago #

    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.

  8. bcworkz
    Member
    Posted 1 year ago #

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

  9. s_ghosh
    Member
    Posted 1 year ago #

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

    Thanks.

  10. s_ghosh
    Member
    Posted 1 year ago #

    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.

  11. bcworkz
    Member
    Posted 1 year ago #

    :D
    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.

  12. s_ghosh
    Member
    Posted 1 year ago #

    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.

  13. bcworkz
    Member
    Posted 1 year ago #

    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).

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags