PHP + Wordpress: Auto list image name into dropdown slection - php

I am creating wordpress theme option panel and want to use some icons. I have one directory dedicated for icon into my theme folder. What I want to do is if user add any new image into that folder it will automatic appear into dropdown selection list into theme option panel.
Is there any way to do this in PHP with Wordpress? I believe that is possible as I saw one theme has the same option but it was so complex so couldn't figured out that and don't remember theme name too now.
I have to use it with below type of code
$video_tax = array(-1 => 'Choose a category');
$video_terms = get_terms('video_category');
if ($video_terms) {
foreach ($video_terms as $video_term) {
$video_tax[$video_term->term_id] = $video_term->name;
}
}

You might want to start by having a look at scandir. This will list all the contents of a folder on your system. From there it would just be a matter of putting the correct path, url or whatever you want in the the value of your options.
EDIT: Here's some sample code from one of my plugins:
function icons_meta(){
global $post;
$custom = get_post_custom($post->ID);
$link = $custom["icon"][0];
$files = scandir(PATH."/icons");
$selected = '';
echo "<select name='icon'>";
foreach($files as $file){
if($file == $link){
$selected = 'selected="selected"';
} else {
$selected = '';
}
echo "<option value='$file' $selected>".$file."</option>";
}
echo "</select>";
}

Related

How come I cannot use bp_core_fetch_avatar() in the theme after I add filter in the plugin, wordpress buddypress?

After I do the add_filter code below in the first plugin, I cannot use bp_core_fetch_avatar() in the second plugin or in the theme, it echo out the same avatar for every user, why? How can I fix this? get_avatar can echo out different avatar base on the user except that it cannot recognize the $gender that I create in the plugin which will tell if it is a female or male, so avatar will assign base on gender . I am trying to pass some parameter like $gender that I created in the plugin, so that is why I think I should figure out to use bp_core_fetch_avatar(), by the way, does get_avatar can pass a parameter from a plugin?, I know get_avatar( $id, $size,$default, $alt). Anyway, I want to know why bp_core_fetch_avatar() echo out same avatar for every user after do the add_filter, I already add 'item_id'=>"$id". Thanks
<?php
add_filter('bp_core_fetch_avatar',array($this,'set_buddypress_avatar'), 10, 1);
?>
<?php
public function set_buddypress_avatar($html_data = ''){
$html_doc = new DOMDocument();
$html_doc->loadHTML($html_data);
$image = $html_doc->getElementsByTagName('img');
foreach($image as $data) {
$original_image = $data->getAttribute('src');
$size = $data->getAttribute('width');
$alt = $data->getAttribute('alt');
if (stripos($alt, 'Profile picture of ') === 0){ // if our alt attribute has "profile picture of" in the beginning...
$name = str_replace('Profile picture of ', '', $alt);
} else if (stripos($alt, 'Profile photo of ') === 0){ // or profile photo of...
$name = str_replace('Profile photo of ', '', $alt);
} else { // if there is some problem - just assign alt to name
$name = $alt;
}
}
?>
I want to know why bp_core_fetch_avatar() echo out same avatar for
every user after do the add_filter
Because you added a filter to a core BP function.
Try removing the filter after you are finished with it in your class.
remove_filter#Example

Best way to have a variable template in php

I'm coding a menu bar for a website in php. Because I don't want to have to edit it multiple times on the half a dozen or so pages I'll have I've decided to put it in it's own separate header.php file and just include_once(header.php) in the various pages.
My problem is that the menu is going to be slightly different depending on which page it's included in. Right now I'm dealing with it by having the following in my header.php file with $PageTitle being defined in the individual pages:
if ($PageTitle == "Home"){
echo '<li class="active">Home</li>';
}
else{
echo '<li>Home</li>';
}
if ($PageTitle == "About"){
echo '<li class="active">About</li>';
}
else{
echo '<li>About</li>';
}
...
The active class simply highlights the menu of the current page (Like the menu bar on the top of StackOverflow). It works fine but I'm curious if there is a better perhaps more efficient way to doing this. Thanks guys.
Try this:
//list of menu headers
$headers = new array();
//populate the array with your headers here ...
foreach($headers as $val)
{
if( $PageTitle == $val )
echo '<li class="active">'.$val.'</li>';
else
echo '<li>'.$val.'</li>';
}
for current class you can also use jquery if you want to:
$(function(){
var path = location.href;
if ( path )
$('.side_menu a[href="' + path + '"]').attr('class', 'current');
});

Use php to create a dynamic menu without a database using only folders in the directory?

I would like to thank anyone who is able to help me in any way possible, I am extremely new to php/coding in general so I am not even sure if I am on the right path.
I wanted to know if it was possible to create 2 step or dynamic drop down menu using only php and html that populates the first drop down menu with folders from a directory.
So far I have
<?php
// Set the path of the dir you want displayed...
$path="./track";
$handle=opendir($path);
while ($file=readdir($handle))
{
echo "\t<option value='".$file."'>".$file."</option>\n";
}
?>
This lets me grab the the folders from a directory, how would I proceed to create a second drop down menu that would let you choose any of the files from whichever folder you selected in the first drop down menu, and then display it on the website?
An example picture of what I am trying to achieve is as follows:
http://postimg.org/image/im03tjh0d/
<?php
/**
* Check if we have submit the folder
* Check if we have submit the file
*/
$post_folder = isset($_POST['folder']) ? $_POST['folder']: null;
$post_file = isset($_POST['file_name']) ? $_POST['file_name'] : null;
/**
* Built out the Folder submission form
*/
// Set the path of the dir you want displayed...
$path="./";
echo '<form method="POST" action="', $_SERVER['PHP_SELF'],'">';
echo '<select name="folder">';
foreach (new DirectoryIterator($path) as $asset){
if($asset->isDot()) continue;
if($asset->isDir()){
/**
* If we've posted which folder we want to view the contents of
* then automatically make that selected in the dropdown on page load
*/
$selected = (isset($post_folder) && $post_folder == $asset->getFileName()) ? 'selected' : '';
echo "\t<option value='".$asset->getFilename()."' $selected>".$asset->getFilename()."</option>\n";
}
}
echo '</select>';
if(isset($post_folder)){
echo '<select name="file_name">';
$folder_to_iterate = $path.$post_folder;
foreach (new DirectoryIterator($folder_to_iterate) as $asset){
if($asset->isFile()){
/**
* If we've posted which folder we want to view the contents of
* then automatically make that selected in the dropdown on page load
*/
$selected = (isset($post_file) && $post_file == $asset->getFileName()) ? 'selected' : '';
echo "\t<option value='".$asset->getFilename()."' $selected>".$asset->getFilename()."</option>\n";
}
}
echo '</select>';
}
$btn_text = isset($post_folder) ? 'Show File Contents' : 'Show Folder Contents';
echo '<input type="submit" value="', $btn_text ,'"/>';
echo '</form>';
if(isset($post_file)){
$file_to_read = $path.$post_folder.'/'.$post_file;
echo '<pre>';
echo htmlentities(file_get_contents($file_to_read));
echo '</pre>';
}
Use ajax and the file system functions.

Wordpress Custom Post Template

I'm trying to get Wordpress to include a different template file, within single.php, if it sees a file matching the slug.
The file does exits, path is correct, safe_mode set to OFF...am I missing something?
$dir=get_bloginfo('stylesheet_directory').'/post_tmpl/';
$categories=get_categories();
foreach($categories as $cat){
if(is_file($dir.$cat->slug.".php")){
require($dir.$cat->slug.".php");
}else{
require($dir."default.php");
}
}
can you try with this code?
$dir=get_bloginfo('stylesheet_directory').'/post_tmpl/';
$categories=get_categories();
foreach($categories as $cat){
$temp = $dir.$cat->slug;
if(is_file($temp.".php")){
require($temp.".php");
}else{
require($dir."default.php");
}
}
Thanks

Using media field from Page in an extension

We all may know the following snippet that gets the first entry of the media field of a page to e.g. display a header image on a page.
10 = IMAGE
10.file {
import = uploads/media/
import.data = levelmedia: 0, slide
import.listNum = 0
}
I am creating an extension (plugin, no ItemuserFunc etc) which needs to work with this media. I don't want to process dozens of typoscript directives in php to create this images. How can I use the example above to process the media field through typoscript, using core functionality instead of reinventing the wheel?
So now I'm in my extension code, having an array of page records. How to get further? The code below doesn't provide the functionality I need because I am not working on the page level (like I would when using the TS from above in a TMENU).
$content .= $this->cObj->stdWrap($page['media'], $this->conf['media_stdWrap.']);
Having an array of page records ($pages) with the associative keys same as the field names in the database, mainly the media field, you would simply walk through the array and create the images using the Typoscript configuration.
Example 1 (a programmer sets the rendering)
foreach($pages as $value) {
$mediaField = t3lib_div::trimExplode(',', $value['media']);
if(!$mediaField[0]) continue;
$imageConf = array(
'file' => 'uploads/media/' . $mediaField[0],
);
$content .= $this->cObj->cObjGetSingle('IMAGE', $imageConf);
}
Example 2 (an editor sets the rendering)
foreach($pages as $value) {
$this->cObj->data = $value;
$content .= $this->cObj->stdWrap($value['media'], $this->conf['media_stdWrap.']);
}
It sets the page data so that they are available as field in Typoscript. The configuration would then look something like this:
{
media_stdWrap {
cObject = IMAGE
cObject {
file {
import = uploads/media/
import.field = media
import.listNum = 0
}
}
}
}
Example 3 (combination of the previous 2 examples)
foreach($pages as $value) {
$mediaField = t3lib_div::trimExplode(',', $value['media']);
$mediaOutput = '';
// Creating the output with a default rendering
if($mediaField[0]) {
$imageConf = array(
'file' => 'uploads/media/' . $mediaField[0],
);
$mediaOutput = $this->cObj->cObjGetSingle('IMAGE', $imageConf);
}
// Allowing custom Typoscript to completely modify the media part
if(array_key_exists('media_stdWrap.', $this->conf)) {
$this->cObj->data = $value;
$mediaOutput = $this->cObj->stdWrap($mediaOutput, $this->conf['media_stdWrap.']);
}
$content .= $mediaOutput;
}
NOTE: These examples are completely untested. I've came up with them out of my head.

Categories