Note that there are some explanatory texts on larger screens.

plurals
  1. POWordpress Widget - Need to list Pages
    primarykey
    data
    text
    <p>I have a wordpress widget where im saving the options, Title + Copy + URL</p> <p>The url is generated by listing the wordpress pages</p> <p>The below code saves and works fine on the front end. But in the widget settings panel when you save it reverts back to select page, i need it to display the previously selected item that it saved.</p> <pre><code>// Sidebar CTA Widget class SidebarCTAWidget extends WP_Widget { function SidebarCTAWidget() { $widget_ops = array('classname' =&gt; 'SidebarCTAWidget', 'description' =&gt; 'Editable Sidebar CTAs' ); $this-&gt;WP_Widget('SidebarCTAWidget', 'Sidebar Call to Action', $widget_ops); } function form($instance) { $defaults = array( 'title' =&gt; '', 'copy' =&gt; '', 'url' =&gt; ''); $instance = wp_parse_args( (array) $instance, $defaults ); $title = $instance['title']; $copy = $instance['copy']; $url = $instance['url']; ?&gt; &lt;p&gt; &lt;label for="&lt;?php echo $this-&gt;get_field_id('title'); ?&gt;"&gt;Title: &lt;input class="widefat" id="&lt;?php echo $this-&gt;get_field_id('title'); ?&gt;" name="&lt;?php echo $this-&gt;get_field_name('title'); ?&gt;" type="text" value="&lt;?php echo attribute_escape($title); ?&gt;" /&gt; &lt;/label&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="&lt;?php echo $this-&gt;get_field_id('copy'); ?&gt;"&gt;Copy: &lt;textarea rows="5" class="widefat" name="&lt;?php echo $this-&gt;get_field_name( 'copy' ); ?&gt;" / &gt;&lt;?php echo esc_attr( $copy ); ?&gt;&lt;/textarea&gt; &lt;/label&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="&lt;?php echo $this-&gt;get_field_id('url'); ?&gt;"&gt;URL: &lt;select class="widefat" id="&lt;?php echo $this-&gt;get_field_id('url'); ?&gt;" name="&lt;?php echo $this-&gt;get_field_name('url'); ?&gt;" name="page-dropdown"&gt; &lt;option value=""&gt; &lt;?php echo esc_attr( __( 'Select Page' ) ); ?&gt;&lt;/option&gt; &lt;?php $pages = get_pages(); foreach ( $pages as $page ) { $option = '&lt;option value="' . get_page_link( $page-&gt;ID ) . '"&gt;'; $option .= $page-&gt;post_title; $option .= '&lt;/option&gt;'; echo $option; } ?&gt; &lt;/select&gt; &lt;/label&gt; &lt;/p&gt; &lt;?php } //save the widget settings function update($new_instance, $old_instance) { $instance = $old_instance; $instance['title'] = strip_tags( $new_instance['title'] ); $instance['copy'] = strip_tags( $new_instance['copy'] ); $instance['url'] = strip_tags( $new_instance['url'] ); return $instance; } function widget($args, $instance){ extract($args, EXTR_SKIP); echo $before_widget; $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']); $copy = empty($instance['copy']) ? ' ' : apply_filters('widget_title', $instance['copy']); $url = empty($instance['url']) ? ' ' : apply_filters('widget_title', $instance['url']); if (!empty($title)) echo '&lt;div class="clearfix sidebar-content"&gt;&lt;h3&gt;' . $title . '&lt;/h3&gt;'; ; // This is the HTML echo '&lt;p&gt;' . $copy . '&lt;/p&gt;&lt;/div&gt;'; echo '&lt;p class="sidebar-link"&gt;&lt;a href="' . $url . '" title="Find out more"&gt;Find Out More&lt;/a&gt;&lt;/p&gt;'; // END echo $after_widget; } } add_action( 'widgets_init', create_function('', 'return register_widget("SidebarCTAWidget");') ); </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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