Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling a function within foreach?
    primarykey
    data
    text
    <p>I've been working on a Wordpress theme project over the past few days and I've been stuck on how to get a dynamic options page running properly using OOP (I'm primarily a Theme Developer, not a PHP scripter).</p> <pre><code> &lt;?php $footerpage = new optionpage; $footerpage-&gt;title = 'Footer'; $footerpage-&gt;titleprint = ' Footer Options'; $footerpage-&gt;slug = 'footer'; $footerpage-&gt;html = array( '1' =&gt; array( 'type' =&gt; 'textarea', 'class' =&gt; 'large-text', 'name' =&gt; 'html', 'title' =&gt; 'HTML', 'description' =&gt; 'Type in whatever HTML you\'d like to see in the footer here:', ), '2' =&gt; array( 'type' =&gt; 'input', 'class' =&gt; 'large-text', 'name' =&gt; 'background-color', 'title' =&gt; 'Background Color', 'description' =&gt; ' Choose a Background Color:' ), ); class optionpage { public $title; public $titleprint; public $slug; public $html = array(); ...... ...... ...... public function ab_settings() { register_setting( $this-&gt;slug, 'ab_options'); add_settings_section('ab_section', '', array(&amp;$this, 'ab_do_titleprint'), 'ab_' . $this-&gt;slug . '_options', 'ab_options' ); foreach ($this-&gt;html as $key =&gt; $html) { add_settings_field( $key, $html['title'], array(&amp;$this, 'ab_do_htmlprint' ), 'ab_' . $this-&gt;slug . '_options', 'ab_section'); } } public function ab_do_htmlprint() { $html = $this-&gt;html[$key]; ?&gt; &lt;p&gt;&lt;?php echo $html['description'] ?&gt;&lt;/p&gt; &lt;&lt;?php echo $html['type'] ?&gt; id="&lt;?php echo $html['id'] ?&gt;" class="&lt;?php echo $html['class'] ?&gt;" name="&lt;?php echo $html['name'] ?&gt;"&gt; &lt;?php get_option ($html['name'])?&gt; &lt;/&lt;?php echo $html['type'] ?&gt;&gt; &lt;?php } ...... ...... ?&gt; </code></pre> <p>In this code example, I'm trying to get the function ab_do_htmlprint to recognize the foreach expressions where it's been called, because the function is going to be called as many times as needed in the foreach loop.</p> <p>I've tried several things, like appending a variable to the function name, but that would require multiple functions of the same code, just with a different name. I also tried passing various variables by reference and such, however they weren't working either, and I may not have been doing that correctly, if they're even needed.</p> <p>Anyway to accomplish this efficiently?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload