views:

40

answers:

2

Hi, I'm only really a novice with PHP (and I know I'm sortof trying to run before I can walk), but I would like to know if it is possible to define a functions name via a textfield.

Currently, I am working with Wordpress so I can use add_option to add something to the database to store data, etc, which is handy. My idea is for a slideshow plugin which allows you to have unlimited slideshows.

It would work by typing the name into the text box then clicking the submit button to store the name. This name would then be given to a function which can be used to display that specific slideshow. Is this possible?

Sorry if this seems confusing. I know what I want to achieve, I just don't have a clue how (but I am willing to learn). Thanks.

A: 

Why do you need new functions? Sounds more like the same funxtion with different arguments, which can easily be your textfield value

Nicolas78
How can you can do that?
Matthew Ruddy
sorry for brevity was on mobile and hoping it might be enough ;). kevtrout's example pretty much points it out: instead of creating new functions like "my_slideshow()", you create a single function like display_slideshow with parameter $slideshowname containing, e.g., "my_slideshow"- what's different between slideshows is anyway the data, not the execution logic, so you don't actually need to define new functions but pass on different data.
Nicolas78
So the argument is, you don't need to do what you were asking for. I'm not PHP savvy enough however to answer the other part of the question; can you create functions whose name is based on a variable name? I'd suppose no, but I'd love to hear a more qualified comment.
Nicolas78
php does support variable functions. If a variable has parens appended to it, php will look for a registered function with that name. $foo() will cause php to look for a function named whatever $foo represents. I don't know this for sure, but I assume you'd have to assign a value prior to the function being registered. More info: http://php.net/manual/en/functions.variable-functions.php
kevtrout
A: 

1.your plugin admin page allows naming and creating slideshows, like creating a post or a gallery with a title, and adding images.

2.you place a function call in your theme where you want a slideshow to appear:

if(function_exists('display_slideshow')){display_slideshow($slideshow_name)};

3.the function display_slideshow() is included in your plugin file.

function display_slideshow($slideshow_name){
            //get slideshow stuff from the database, 
            //using the name of the show passed as arg
            //write your html and php to show the images the way you design
        }
kevtrout
Wordpress uses the function add_option to add an option to the database. So lets say I give the textfield the name "title" (name="title") then when the user clicks submit it adds the option named from whatever the user wrote in the text box;$option_name = $_REQUEST['title'];add_option($option_name,'',''); // Add an option with the name that the user has typed to the databaseHowever I then cannot figure out how get the name of the option that has just been created then display a specific function that uses that name to show the content of that particular slideshow.
Matthew Ruddy
Please edit your question to explain how the user creates the slideshow, and how the slideshow gets added to a page or post or sidebar. Will the user have to hardcode a function like I suggested? or will they use a shortcode in a post, or will you be supplying an 'insert slideshow' button in the post composition panel? These two questions are very important to give you a better answer. Right now, you are explaining a detailed part of your plan, and I'd like to understand the big picture.
kevtrout
You know about *get_option(option name)* right?
kevtrout
Yeah I do know about get_option although I don't believe this can be used for this kind of system because the slideshows name is defined by the user using a textbox.The idea is that the user will type the name they wish to call their slideshow into a textbox. Then they press 'submit' which sends that name to a database table where it is stored. Then, an options panel needs to be created titled by whatever the user wrote into the textbox, followed by the slideshows options where the user can change them. It is then displayed using either php code or shortcode such as [slider name=myslider].
Matthew Ruddy
I don't believe you need a function with a variable name to accomplish this. You need a function to load the show creation form that accepts the slideshow name as an argument, similar to my main answer above. The function would retrieve the slideshow name, and load the show creation form. Once the show is saved, another function can create the shortcode or php code to insert, based on the slideshow name or some other ID field you create.
kevtrout