WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How can I enable WYSIWYG view for custom fields (17 posts)

  1. TransPersonal
    Member
    Posted 4 years ago #

    I usually use html view exclusively but I need the custom fields to work in WYSYWIG mode. Because I use custom fields to post references in the form of urls and since custom fields seem to work just in html mode the urls are not transformed into hyperlinks. I don't feel like going through each url and adding <a href... to them one by one. How can I make custom fields work in WYSYWYG mode?

    P.S. changing tinymce's mode from html to "view" doesn't do it either :(

  2. TransPersonal
    Member
    Posted 4 years ago #

    In other words: How do your auto hyperlink URLS posted via custom fields?

  3. esmi
    Theme Diva & Forum Moderator
    Posted 4 years ago #

    Add the link markup via the template when you display the custom field(s).

  4. wordcastpodcast
    Member
    Posted 4 years ago #

    You can use the Custom Field Templates plugin and create a field with TinyMCE (WYSIWYG editor WordPress uses).

  5. TransPersonal
    Member
    Posted 4 years ago #

    Sorry guys but the Custom Field Template plugin isn't doing it. It's helpful in other ways but for whatever reason it doesn't auto-hyperlink urls.

    I tried using another plugin that does do that but it works only on post and comment content (doesn't auto-link custom field urls).

  6. Mark / t31os
    Moderator
    Posted 4 years ago #

    As Esmi said..

    Add the link markup via the template when you display the custom field(s).

    Like so...

    <?php $yourfield = get_post_meta($post->ID,'yourfieldname',true);
    if($yourfield) echo '<a href="'.$yourfield.'">'.$yourfield.'</a>'
    ?>

    There's a number of ways, that's just one..

    You'd add that to whichever file is handling the given page you are viewing, examples would be index.php, single.php, page.php (you get the point).

  7. TransPersonal
    Member
    Posted 4 years ago #

    Thanks guys, I admit I wasn't sure as to what esmi had meant earlier.

    t31os_: before I had two consecutive text urls like this:

    <p>http://google.com</p>
    <p>http://yahoo.com</p>

    after employing your code it now looks like this:

    http://google.com</p&gt;
    <p>http://yahoo.com</p>
    "><p>http://google.com</p>
    <p>http://yahoo.com</p>

    As you can see the paragraph tags are causing problems (which I didn't put in the first place but wordpress did automatically). Aside from that the two consecutive links have become one i.e. they are both within the same link tag. Is there any way to fix this (sorry, php is something I'm still struggling with).

    Thanks

  8. TransPersonal
    Member
    Posted 4 years ago #

    there was an:

    before the http://goo...

  9. TransPersonal
    Member
    Posted 4 years ago #

    dammit wordpress keeps hyperlinks the code :p

  10. esmi
    Theme Diva & Forum Moderator
    Posted 4 years ago #

    Use backticks to enclose code or markup. Sorry I was so brief earlier. I was being called away.

    I'm confused as to why WP is adding paragraph tags. As far as I was aware, get_post_meta doesn't go through any formatting filters. Does:

    <?php $yourfield = get_post_meta($post->ID,'yourfieldname',true);
    if($yourfield) echo '<a href="'.$yourfield.'">'.strip_tags($yourfield).'</a>'
    ?>

    help?

  11. Mark / t31os
    Moderator
    Posted 4 years ago #

    What is the value of this field (take any one you have as an example), are you storing multiple url's in the 1 field?

  12. TransPersonal
    Member
    Posted 4 years ago #

    Thank you esmi, it was the Custom field template plugin's TinyMce editor that was adding the pagaraph tags. I deactivated it now and am using the default custom fields. The code you posted does work but only for single urls. If I post two textually like this:

    http://google.com<br />
    http://yahoo.com

    it is turned in into this:

    <a href="http://people.com%0A%3Cbr/%3E%0Ahttp://google.com">http://people.com%0A%3Cbr/%3E%0Ahttp://google.com</a>

    again they are both inside the same link tag...

  13. TransPersonal
    Member
    Posted 4 years ago #

    t31os_: the value is called website and you just reminded me, I am using the same field for multiple urls. I will try using different fields and see how it goes...

  14. TransPersonal
    Member
    Posted 4 years ago #

    Apparently posting the urls one at a time in the custom fields only makes the first one appear and not the rest...

  15. esmi
    Theme Diva & Forum Moderator
    Posted 4 years ago #

    You need to enter each url separately - though you could call each field "website" and have 2 or more per post. Then you'd need something like:

    <?php $yourfield = get_post_meta($post->ID,'website',false);
    if(count($yourfield) <0) {
    	foreach($yourfield as $this_one) {
    		echo '<a href="'.$this_one.'">'.$this_one.'</a>'
    	}
    }
    ?>
  16. TransPersonal
    Member
    Posted 4 years ago #

    esmi that code gives me a parse error, I guess I can just rename the custom fields to website1, website2 and so on and call them using the earlier code.

  17. TransPersonal
    Member
    Posted 4 years ago #

    Thanks a lot esmi and t31os_. This is what I'm doing now. I am using this in my template:

    <?php $website1 = get_post_meta($post->ID,'website1',true);
    if($website1) echo '<a href="'.$website1.'">'.strip_tags($website1).'</a>'
    ?>
    <br />
    <?php $website2 = get_post_meta($post->ID,'website2',true);
    if($website2) echo '<a href="'.$website2.'">'.strip_tags($website2).'</a>'
    ?>
    <br />
    <?php $website3 = get_post_meta($post->ID,'website3',true);
    if($website3) echo '<a href="'.$website3.'">'.strip_tags($website3).'</a>'
    ?>

    and this in the Custom Field Template (with the "In case that you would like to add p and br tags in textareas automatically" option unchecked):

    [website1]
    type = textfield
    size = 35
    hideKey = true
    
    [website2]
    type = textfield
    size = 35
    hideKey = true
    
    [website3]
    type = textfield
    size = 35
    hideKey = true

    This works great. Thanks again guys for the help!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.