- This topic is empty.
-
AuthorPosts
-
September 25, 2010 at 7:32 pm #30362i0nyyMember
Hello. I want to create a WP Plugin and I have a problem.
The people that will use the plugin can configure the plugin from the dashboard. In the dashboard they can enter id`s of some posts. e.g: 1, 324, 34
i.e:
$post_id = 1;
$posts_for_plugin = “1,324.34”;I need something like
if ($post_id is somewhere in $posts_for_plugin)
{
return something;
}else{
return something else;
}
If it was a single value it would be something like
if ($post_id ==$posts_for_plugin )
but with more than one value, I dont`t know what to do.
I tried using
array($posts_for_plugins)
but the array is something like
array[0] = 1, 324, 34
so it`s useless
Hope anyone understood an can help me.
Thanks!September 26, 2010 at 6:47 am #79019BobMemberYou can use the in_array() function for that. Here’s an example:
$post_id = 1;
$posts_for_plugin = array(1,324,34);
if ( in_array($post_id, $posts_for_plugin) ) {
echo "Yep, it's in the array";
}
else {
echo "Sorry, not in array";
}
?>The in_array function has 2 parameters here (you can also set a third). The first parameter is the string you’re looking for in the array, which is $post_id here. The second parameter is the array in which the first parameter has to be found, here that is $posts_for_plugin. The second parameter ofcourse has to be an array.
September 26, 2010 at 7:12 am #79012i0nyyMemberThe $posts_for_plugins values will be set from the dashboard, so
$posts_for_plugin will be something like:
e>
$posts_for_plugin = "1,324.34";
if i try to insert $posts_for_plugin in an array it won`t work…
How can i make this:
$posts_for_plugin = "1,324.34";
to be an array?
September 26, 2010 at 7:28 am #79011BobMemberCan’t you just do this, just add array(…) to it? Like this:
$posts_for_plugin = array(1,324,34);
I don’t know for sure though, I don’t know a whole lot about PHP.
Also, how exactly are they set from the dashboard? Perhaps you can make it so that it will be inserted in an array immediately?
September 28, 2010 at 12:39 am #78968willBMemberIf you use explode():
$string = "1,324,34";
$posts_for_plugin = explode(',', $string);
You can pretty much leave your code that is getting the string from the dashboard the same.
So I guess in full if would be something like:
$post_id = 1;
$string = "1,324,34";
$posts_for_plugin = explode(',', $string);
if ( in_array($post_id, $posts_for_plugin) )
{
echo "Yep, it's in the array";
}
else
{
echo "Sorry, not in array";
}
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.