0

For a current project I am trying to echo a line of text saved in the theme's options page. I use the wp_editor function for the user input instead of a textarea. The code I am using is quite simple:

    <?php
    break;
    case "wysiwyg" : 
    ?>
     <tr valign="top">
       <th scope="row"><?php echo $value['name']; ?>:</th>
       <td>
          <?php
          if( get_settings($value['id']) != "") {
                $settings = stripslashes(get_settings($value['id']));
          }else{
              $settings = $value['std'];
          }
          ?>
          <?php wp_editor($settings,$value['id'],array('wpautop' => true,'media_buttons'=>false)); ?>
          <br /><?php echo $value['desc']; ?>
         </td>
        </tr>
    <?php break;
    ?>

I tried to make sure paragraphs are used by adding wpautop => true.. The problem occurs when I try to add a single line of text in the WYSIWYG-mode. When saving the field, paragraph's arent added. After reloading the page the html view is changed back to WYSIWYG and all my HTML tags have disappeared.

I tried to use the wpautop function manually, but this would append a blank paragraph after the insert variable. My code looks like this:

<?php echo wpautop(stripslashes($lo_homepage_highlight_1_text)) ;?>

And will return:

<p>Lorem ipsum (.....) dolor sit amet. Great rich text</p>
<p></p>

I don't understand what I am missing here.

6
  • do you have a blank line after your line of text?
    – mor7ifer
    Commented Mar 2, 2012 at 17:54
  • What do you exactly mean? There is actually some extra space because of the bottom margin from the extra element.. And I insert only one rule of text in the wp_editor..
    – Ewald
    Commented Mar 2, 2012 at 21:15
  • In TinyMCE, is there a break line after your single line of text? wpautop just turns \n to <p> basically...which can be pretty inconvenient at times.
    – mor7ifer
    Commented Mar 3, 2012 at 2:25
  • Yes there was, but every element saved had one.I editted my question above with my found solution. Somehow the save function didnt work properly.
    – Ewald
    Commented Mar 10, 2012 at 16:02
  • You need to submit your answer as an answer and mark it as solved, not add the solution to the question.
    – mor7ifer
    Commented Mar 10, 2012 at 16:43

1 Answer 1

2

After doing some more research I found out that my save function does not work well. I think I should apply wpautop and wptexturize to those entries. My save function looks like this:

        if ( 'save' == $_REQUEST['action'] ) {
            foreach ($options as $value) {
                update_option( $value['id'], $_REQUEST[ $value['id'] ] ); }
            foreach ($options as $value) {
                if( isset( $_REQUEST[ $value['id'] ] ) ) { update_option( $value['id'], $_REQUEST[ $value['id'] ]  ); } else { delete_option( $value['id'] ); } }
            header("Location: themes.php?page=functions.php&saved=true");
            die;
    }

Since the wp_editor is defined in the array as wysiwyg i should be able to get the GET-request to determine whether the field is sent from the wp_editor. I adjusted the code to and now it works:

        if ( 'save' == $_REQUEST['action'] ) {
            foreach ($options as $value) {
                if ($_REQUEST[$value['type']] == 'wysiwyg' ) {
                    update_option( $value['id'], wpautop(wptexturize($_REQUEST[ $value['id'] ] ))); 
                } else {
                    update_option( $value['id'], $_REQUEST[ $value['id'] ] ); 
                }               
                foreach ($options as $value) {
                    if( isset( $_REQUEST[ $value['id'] ] ) ) {
                        if($_REQUEST[$value['type']] == 'wysiwyg') {
                            update_option( $value['id'], wpautop(wptexturize($_REQUEST[ $value['id'] ] )));
                        }
                        else {
                            update_option( $value['id'], $_REQUEST[ $value['id'] ] );
                        }
                    } else {
                        delete_option( $value['id'] );
                    } 
                }
            }
            header("Location: themes.php?page=functions.php&saved=true");
            die;
    }

The code might use some improvements, but it works for now :)

1
  • hopefully helpful hint for next time: wpautop() is meant to be used only right before you put the content on the template. You shouldn't need to use wpautop() on the admin side, before it's saved to the database. That can possibly cause problems and unexpected behavior later down the line. The WP-Editor doesn't play nice with extra html and should work fine with just straight line breaks - just wrap your option text in wpautop() right before you use it on the front end.
    – Jen
    Commented Oct 12, 2012 at 3:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.