Page 1 of 1
Get a hidden variable from a wordpress post
Posted: Tue Jul 03, 2007 9:32 am
by dzynit
Since it has become difficult to find a plugin for wordpress that currently works with the latest version in allowing php code to be used in posts and pages and then executed, I want to use a work-around. I've been reading through the forum all morning, but haven't found exactly what answers my question. So I apologize if I am reasking a previously answered question. - or just plain asking an obvious, should already know how to do question.
I want to allow a user of a wordpress blog to post a page, use the 'code' tags that wordpress has, to hold a url that I can have my template pull into a variable.
Yes, it will be safe to do this in this particular case because it is a blog that only 2 people have access to work in.
All I want to do is see the correct php code to find a section of text between 2 tags or 2 set character combinations that can be used for the php to search for. Then have that section of text set into a variable and then that entire section from beginning tag to end tag, just removed during the display of the post.
Then I can use the changing variable from post to post to execute the correct php code that I have hard-coded in my template.
I appreciate any help on this and hope that I've made it understood what I need.
Re: Get a hidden variable from a wordpress post
Posted: Tue Jul 03, 2007 11:54 am
by RobertGonzalez
dzynit wrote:I want to allow a user of a wordpress blog to post a page, use the 'code' tags that wordpress has, to hold a url that I can have my template pull into a variable.
...
All I want to do is see the correct php code to find a section of text between 2 tags or 2 set character combinations that can be used for the php to search for. Then have that section of text set into a variable and then that entire section from beginning tag to end tag, just removed during the display of the post.
Then I can use the changing variable from post to post to execute the correct php code that I have hard-coded in my template.
I am a little lost. Can you explain this a little more clearly?
Posted: Tue Jul 03, 2007 11:59 am
by Ambush Commander
All I want to do is see the correct php code to find a section of text between 2 tags or 2 set character combinations that can be used for the php to search for.
Looks like you want a regexp. Have you tried anything yet?
Posted: Fri Jul 06, 2007 11:41 am
by dzynit
Here is a section out of the html page that shows where I need to parse out to a variable and remove the line of code. I need to extract the url link text from between the <xxxx> tags. (These are the code tags from wordpress - I substituted with x's because this forum uses the same tag for post displays.)
code:
<td width="100%" bgcolor="#ffffff" style="margin:8px;" align="left" valign="top">
<div class="entry">
<p><xxxx>
http://www.awebsite.com/feed</xxxx><br />
This would be the regular post text.</p>
</div>
<br><p>
I need to use php to get that piece of text into a variable, then erase it from the html code so that it does not display to the viewers.
This will allow me to use php to display the variable later in the post. The person who will be creating the posts can understand how to insert that link at the top of the post, but not how to change code, files, php, etc that would have to be done often to create a new template page in the wordpress theme each time they make a new post.
Does this help explain better?
Posted: Fri Jul 06, 2007 11:50 am
by Ambush Commander
Yeah. Use a preg_replace_all() with the $matches array and an empty substitution set. I won't give you the regex though, it's a helpful skill to learn.
Posted: Fri Jul 06, 2007 11:55 am
by dzynit
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
This is what I have been trying to use, but don't seem to be having any luck: (Again, I have substituted x's for the word code.) This code is placed after the post should display - simply for the ability to test to get it working.
Code: Select all
<?php
$input = the_content();
function parseTagsRecursive($input)
{
$regex = '#\[xxxx]((?:[^[]|\[(?!/?xxxx])|(?R))+)\[/xxxx]#';
if (is_array($input)) {
$input = $input[0];
}
return preg_replace_callback($regex, 'parseTagsRecursive', $input);
}
$output = parseTagsRecursive($input);
echo "OUTPUT = " . $output;
?>
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Fri Jul 06, 2007 11:59 am
by Ambush Commander
There's no need to be that complicated. We're not dealing with a nested tag structure: it's a simple <XXXX>URL</XXXX> that needs to be parsed out of the document.
Posted: Fri Jul 06, 2007 12:07 pm
by dzynit
I'm kind of in a bind here and have to move on to the next project. Can you at least point me in the right direction for some kind of tutorial to learn this from.
That last piece of code was the only thing I found that looked somewhat like what I needed it to do. This isn't something I need every day - would just make things easier than creating new templates on a daily basis.
Posted: Fri Jul 06, 2007 12:28 pm
by superdezign
AC is telling you that recursion is completely unneeded. This isn't tokenization, it's replacement.
Posted: Fri Jul 06, 2007 12:34 pm
by dzynit
Sorry. I was hoping for a fast answer and a quick fix. I have too much on my plate now to start learning from the beginning on this part of php.
I know what I have to know to keep wordpress in my daily projects.
I can just have it outsourced.
Thanks for everyone's help. I do appreciate your time. I will definitely be back when I have the time to go through the learning process to expand. As a matter of fact - the new person I hired is suppose to start next week to start learning new things - she'll be the one here learning this and asking questions.
Thanks a bunch, sorry for taking up time.
Posted: Fri Jul 06, 2007 1:08 pm
by superdezign
....?
We're telling you that it's easier than you're making it. It's a simple preg_replace. No callback. No recursion. Just preg_replace. You're regex is over complicated as well. Just get everything between the tags an only keep the data between them. The tags can't be nested.
Posted: Fri Jul 06, 2007 3:07 pm
by Ambush Commander
What he's saying is that he doesn't have time to figure out a fix himself, and that he was hoping that one of us would give the answer to him.
Posted: Fri Jul 06, 2007 3:22 pm
by superdezign
Ambush Commander wrote:What he's saying is that he doesn't have time to figure out a fix himself, and that he was hoping that one of us would give the answer to him.
Hehe. Wrong forum, then.
