• 0

WordPress export list of pages in to an array.


Question

I am trying to export a list of pages from the database as an array for WordPress. This is to generate a dropdown menu of the pages from the database.

I'm a little stuck as this part of WordPress is new to me and I need a little help from any one who has had experience in creating something similar to this function.

This is my function:

function fetch_redirect_page(){
	global $wpdb;
	$pages = $wpdb->get_results("SELECT post_title, post_status, post_type FROM $wpdb->posts WHERE `post_status`='publish' AND `post_type`='page'");
	$redirect = "";
	foreach($pages as $page){
		$redirect .= '"'.$page->post_title.'"';
	}
	$redirect = array($redirect);
	return $redirect;
}

to which goes into the options section of this array.

array(
	"name" => "Redirect To",
	"desc" => "This will redirect the user to the page selected after the user has left a comment.",
	"id" => "commentredirect",
	"type" => "select",
	"options" => fetch_redirect_page(),
	"std" => ""
),

This is my html export results so far:

<label for="commentredirect">Redirect To</label>
<select name="commentredirect" id="commentredirect">
<option value="" about="" home="" blog="" contact="">"About""Home""Blog""Contact"</option>
</select>
<small>This will redirect the user to the page selected after the user has left a comment.</small>

which is completely wrong and will not work at all.

I need it to export like so:

<label for="commentredirect">Redirect To</label>
<select name="commentredirect" id="commentredirect">
<option value="about">About</option>
<option value="blog">Blog</option>
<option value="contact">Contact</option>
<option value="home">Home</option>
</select>
<small>This will redirect the user to the page selected after the user has left a comment.</small>

If any one can help me with this, it would very much appreciated.

Thank you.

Link to comment
Share on other sites

15 answers to this question

Recommended Posts

  • 0

I'm trying to include it on a Themes Option page so that the function is already available to my users.

I don't want the plugin and I know WordPress has a drop down function already but it is not suitable for this section.

So if you can help me with what I have asked instead that would be helpful.

Thank you.

Link to comment
Share on other sites

  • 0

I didn't say to use the plugin... I said to just reference the links so you can utilize some of the functionality that's already there.

I have used the plugin for reference and it didn't help me. So can you help me with this?

Link to comment
Share on other sites

  • 0

Can any one help me on this. Don't think of it just for using in WordPress. Think of it as trying to just create an array inside an array normally. Would really like to get this achieved and appreciate any help possible. Thank you.

Link to comment
Share on other sites

  • 0

Change your function code to


function fetch_redirect_page(){
global $wpdb;
$pages = $wpdb->get_results("SELECT post_title, post_status, post_type FROM $wpdb->posts WHERE `post_status`='publish' AND `post_type`='page'");
$redirect = "";
foreach($pages as $key => $page) {
$redirect .= $page->post_title;
if($key != 0) {
$redirect .= ","
}
}
$redirect = explode(",", $redirect);
return $redirect;
}
[/CODE]

That'll make it into a proper array.

As for printing it into the HTML, if you show me the code you have for printing this section:

[CODE]
<label for="commentredirect">Redirect To</label>
<select name="commentredirect" id="commentredirect">
<option value="" about="" home="" blog="" contact="">"About""Home""Blog""Contact"</option>
</select>
<small>This will redirect the user to the page selected after the user has left a comment.</small>
[/CODE]

I will show you what you need to modify for it to work.

Link to comment
Share on other sites

  • 0

Change your function code to

function fetch_redirect_page(){
        global $wpdb;
        $pages = $wpdb-&gt;get_results("SELECT post_title, post_status, post_type FROM $wpdb-&gt;posts WHERE `post_status`='publish' AND `post_type`='page'");
        $redirect = "";
        foreach($pages as $key =&gt; $page) {
                $redirect .= $page-&gt;post_title;
                if($key != 0) {
                     $redirect .= ","
                }
        }
        $redirect = explode(",", $redirect);
        return $redirect;
}
[/CODE]

That'll make it into a proper array.
As for printing it into the HTML, if you show me the code you have for printing this section:
[CODE]
&lt;label for="commentredirect"&gt;Redirect To&lt;/label&gt;
&lt;select name="commentredirect" id="commentredirect"&gt;
&lt;option value="" about="" home="" blog="" contact=""&gt;"About""Home""Blog""Contact"&lt;/option&gt;
&lt;/select&gt;
&lt;small&gt;This will redirect the user to the page selected after the user has left a comment.&lt;/small&gt;
[/CODE]

I will show you what you need to modify for it to work.

This function is working great already. This is what it prints now after adding the changes.
[code]&lt;select name="commentredirect" id="commentredirect"&gt;
&lt;option value="AboutHome"&gt;AboutHome&lt;/option&gt;
&lt;option value="Blog"&gt;Blog&lt;/option&gt;
&lt;option value="Contact"&gt;Contact&lt;/option&gt;
&lt;option value="" selected="selected"&gt;&lt;/option&gt;
&lt;/select&gt;

I don't know why two of the values have stuck to each other and why the empty value is at the bottom.

This is what it prints if I select one and save it as my option.

&lt;select name="commentredirect" id="commentredirect"&gt;
&lt;option value="AboutHome"&gt;AboutHome&lt;/option&gt;
&lt;option value="Blog" selected="selected"&gt;Blog&lt;/option&gt;
&lt;option value="Contact"&gt;Contact&lt;/option&gt;
&lt;option value="" selected="selected"&gt;&lt;/option&gt;
&lt;/select&gt;

This is what i use for my drop down menus. I have had no problems with it so far.

&lt;label for="&lt;?php echo $value['id']; ?&gt;"&gt;&lt;?php echo $value['name']; ?&gt;&lt;/label&gt;
&lt;select name="&lt;?php echo $value['id']; ?&gt;" id="&lt;?php echo $value['id']; ?&gt;"&gt;
&lt;?php foreach($value['options'] as $option){ ?&gt;
&lt;option value="&lt;?php echo $option; ?&gt;"&lt;?php if(get_settings($value['id']) == $option){ echo ' selected="selected"'; }elseif($option == $value['std']){ echo ' selected="selected"'; } ?&gt;&gt;&lt;?php echo ucwords($option); ?&gt;&lt;/option&gt;&lt;?php } ?&gt;
&lt;/select&gt;

So it's nearly there. What could be causing part of it to go wrong?

Link to comment
Share on other sites

  • 0

I have tweaked it a little so now it echos the drop down menu correctly for each page as a separate option but I still get a blank option which is automatically selected at the bottom of the list.

function fetch_redirect_page(){
	global $wpdb;
	$pages = $wpdb-&gt;get_results("SELECT id, post_title FROM $wpdb-&gt;posts WHERE `post_status`='publish' AND `post_type`='page'");
	$redirect = "";
	foreach($pages as $key =&gt; $page){
		$redirect .= $page-&gt;id." =&gt; ".$page-&gt;post_title;
		if($key =! 0){ $redirect .= ", "; }
	}
	$redirect = explode(", ", $redirect);
	return $redirect;
}

I also need to fetch the page id as the value but I want the title to show as the option.

So far it prints out like this:

&lt;select name="commentredirect" id="commentredirect"&gt;
&lt;option value="2 =&gt; About"&gt;2 =&gt; About&lt;/option&gt;
&lt;option value="4 =&gt; Home"&gt;4 =&gt; Home&lt;/option&gt;
&lt;option value="6 =&gt; Blog"&gt;6 =&gt; Blog&lt;/option&gt;
&lt;option value="13 =&gt; Contact"&gt;13 =&gt; Contact&lt;/option&gt;
&lt;option value="" selected="selected"&gt;&lt;/option&gt;
&lt;/select&gt;

I now need it to print out like this:

&lt;select name="commentredirect" id="commentredirect"&gt;
&lt;option value="2"&gt;About&lt;/option&gt;
&lt;option value="4"&gt;Home&lt;/option&gt;
&lt;option value="6"&gt;Blog&lt;/option&gt;
&lt;option value="13"&gt;Contact&lt;/option&gt;
&lt;/select&gt;

Link to comment
Share on other sites

  • 0

It appears to me that you are seriously over-complicating this problem. Why are you concatenating a string and then trying to get an associative array out of it? Just use an array from the start! :p

&lt;?php
function fetch_redirect_page(){
        $pages = get_pages();
        $redirect = array();
        foreach($pages as $page){
                $redirect[$page-&gt;ID] = $page-&gt;post_title;
        }
        return $redirect;
}
?&gt;

You want $redirect to be an array with page IDs as keys and titles as values, so you just loop over all pages and use those properties as key and value for a new element in $redirect. That's all you ever need, after that you can just return $redirect. :)

Note that I replaced your raw database query with a call to get_pages() which does exactly the same (return all published pages). It is highly recommended to use native WordPress functions wherever you can, especially when you're working with the database. ;)

Link to comment
Share on other sites

  • 0

It appears to me that you are seriously over-complicating this problem. Why are you concatenating a string and then trying to get an associative array out of it? Just use an array from the start! :p

&lt;?php
function fetch_redirect_page(){
        $pages = get_pages();
        $redirect = array();
        foreach($pages as $page){
                $redirect[$page-&gt;ID] = $page-&gt;post_title;
        }
        return $redirect;
}
?&gt;

You want $redirect to be an array with page IDs as keys and titles as values, so you just loop over all pages and use those properties as key and value for a new element in $redirect. That's all you ever need, after that you can just return $redirect. :)

Note that I replaced your raw database query with a call to get_pages() which does exactly the same (return all published pages). It is highly recommended to use native WordPress functions wherever you can, especially when you're working with the database. ;)

That does not give the options value as the page id, it gives me just the title as before. I need the values as the page id and have it display the title for each option.

I need it to display like this!

&lt;select name="commentredirect" id="commentredirect"&gt;
&lt;option value="2"&gt;About&lt;/option&gt;
&lt;option value="4"&gt;Home&lt;/option&gt;
&lt;option value="6"&gt;Blog&lt;/option&gt;
&lt;option value="13"&gt;Contact&lt;/option&gt;
&lt;/select&gt;

Not like this!

&lt;select name="commentredirect" id="commentredirect"&gt;
&lt;option value="About"&gt;About&lt;/option&gt;
&lt;option value="Home"&gt;Home&lt;/option&gt;
&lt;option value="Blog"&gt;Blog&lt;/option&gt;
&lt;option value="Contact"&gt;Contact&lt;/option&gt;
&lt;/select&gt;

Link to comment
Share on other sites

  • 0

It looks to me that the fault is with the function you're passing that array to, I believe the implementation doesn't take array keys in account to generate the <select>.

I did a quick Google search and I found out that this option structure is used in this article about theme options. After going through the code of that article, I found that the <select> generation code does not take array keys as option value attributes.

&lt;td width="80%"&gt;
&lt;select style="width:240px;" name="&lt;?php echo $value['id']; ?&gt;" id="&lt;?php echo $value['id']; ?&gt;"&gt;
&lt;?php foreach ($value['options'] as $option) { ?&gt;
&lt;option&lt;?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?&gt;&gt;&lt;?php echo $option; ?&gt;&lt;/option&gt;
&lt;?php } ?&gt;
&lt;/select&gt;
&lt;/td&gt;

Here, we see that the foreach loop doesn't use a key variable and that the generated HTML doesn't contain a value attribute. My guess is that the value attribute you're seeing in the HTML is copied from a DOM inspector like Firebug which shows the parsed HTML DOM with the missing attributes added.

If all my assumptions are correct, then you should be able to fix this by just changing the piece of code mentioned above with:

&lt;td width="80%"&gt;
&lt;select style="width:240px;" name="&lt;?php echo esc_attr($value['id']); ?&gt;" id="&lt;?php echo esc_attr($value['id']); ?&gt;"&gt;
&lt;?php foreach ($value['options'] as $option_key =&gt; $option_name) { ?&gt;
&lt;option value="&lt;?php echo esc_attr($option_key) ?&gt;"&lt;?php if ( get_settings( $value['id'] ) == $option_key || $value['std'] == $option_key) { echo ' selected="selected"'; } ?&gt;&gt;&lt;?php echo esc_html($option_name); ?&gt;&lt;/option&gt;
&lt;?php } ?&gt;
&lt;/select&gt;
&lt;/td&gt;

Note that I added esc_attr() and esc_html() calls to the echo statements, these are utility functions provided by WordPress which escape characters for HTML output, such as quotes and backslashes. It's a good coding practice to sanitize your input and output, I can guarantee it'll save you headaches. ;)

If this isn't the code you're using, then please provide us with some more details about where your array is used. It is tough for us to help you if we don't know what you're using. ;)

Link to comment
Share on other sites

  • 0

Thank you very much I got the drop down now loading correctly on my options page. Now I need to fix a small problem with my filter.

It seems to be ignoring the new page redirection option and go back to the page or post the user commented on. Please can anyone take a look and see what is missing or incorrect.

This is what I have so far.

// Redirects commenter to another page.
function change_redirect($location){
	global $wpdb, $framework, $comment;
	$cc = $wpdb-&gt;get_var("SELECT COUNT(comment_author_email) FROM $wpdb-&gt;comments WHERE comment_author_email='".$comment-&gt;comment_author_email."'");
	if($cc == 1){
		$location = get_option($framework."_commentredirect");
		return get_permalink($location);
	}
	else{
		return $location;
	}
}
add_filter('comment_post_redirect','change_redirect');

Thank you.

Link to comment
Share on other sites

  • 0

What is the value of that "$framework_comemntredirect" option? Is it an URL or a post ID? get_permalink() only accepts a post ID as its first parameter. Other than that, I don't really see what could be the problem...

Try some debugging. Throw in some echo()s and var_dump()s to monitor the state of your variables during execution. Make sure that your filter gets triggered properly by echo()ing "Hello world" or something.

If you manage to pinpoint the position where your code fails, you might be able to figure it out yourself. If not, post about it here. :)

Link to comment
Share on other sites

  • 0

What is the value of that "$framework_comemntredirect" option? Is it an URL or a post ID? get_permalink() only accepts a post ID as its first parameter. Other than that, I don't really see what could be the problem...

Try some debugging. Throw in some echo()s and var_dump()s to monitor the state of your variables during execution. Make sure that your filter gets triggered properly by echo()ing "Hello world" or something.

If you manage to pinpoint the position where your code fails, you might be able to figure it out yourself. If not, post about it here. :)

It should get the post ID from the options that is saved as "$framework_commentredirect". I checked to see if the option was saved as a post ID and it is.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.