WordPress.org

Ready to get started?Download WordPress

Forums

problem with esc_textarea and get_post_meta (3 posts)

  1. iansane
    Member
    Posted 2 years ago #

    Hi,

    I have a custom meta box in which I want to be able to enter meta tags so I need it to accept html and quotes. I have it accepting them but then when I run my function in header.php to echo them is where I am having problems. I got them to work in the dashboard by using esc_textarea but when I use this to echo out the post meta I get an error.
    htmlspecialchars() expects parameter 1 to be string, array given

    When I don't use esc_textarea() I get an empty string for the array key even though I know it's there looking in the database so I'm guessing it is because of trying to output <meta name='whatever' content='whatever'> with the open and closing tags.

    Here is my function in functions.php that I call in header.php:

    function get_facebook_meta()
    {
    	global $post;
    
    	$my_facebook_meta = esc_textarea(get_post_meta($post->ID, '_my_meta_facebook', false));
    	//$my_facebook_meta = get_post_meta($post->ID, '_my_meta_facebook', false);
    	echo $my_facebook_meta['facebook'];
    	//print_r($my_facebook_meta);
    }

    I left in the commented lines where I'm trying to figure this out. I also tried both true and false as the 3rd param even though false makes no sense since it is returning a serialized array.

    I'm sure the error I'm getting is because the data is a serialized array but I'd like to get around this so I can use the html tags.

    Thanks

  2. iansane
    Member
    Posted 2 years ago #

    Once again, I work on figuring something out for hours and then figure it out after posting the question.

    get_post_meta was returning the array with the value intact. I just used esc_textarea on the single value before echoing it instead of trying to use it on the get_post_meta function.

    function get_facebook_meta()
    {
    	global $post;
    	$my_facebook_meta = get_post_meta($post->ID, '_my_meta_facebook', true);
    	echo esc_textarea($my_facebook_meta['facebook']);
    }

    I'll mark it resolved but any suggestions on doing this better are appreciated.

    Thanks

  3. iansane
    Member
    Posted 2 years ago #

    I thought the problem was resolved.

    I can't seem to put in double quotes or slashes for the html because add_post_meta() is stripping them out.

    Can anyone tell me how to stop that? How would I protect the metabox data from stripping? I tried using addslashes() but that causes the data to not get written to the database at all.

    Here is my code for saving the data:

    $current_data = esc_textarea(get_post_meta($post_id, '_my_meta_facebook', TRUE)); 
    
        $new_data = $_POST['_my_meta_facebook'];
    
        //$new_data = addslashes($new_data);
        //my_meta_facebook_clean($new_data);
    
        if ($current_data)
        {
            if (is_null($new_data)) delete_post_meta($post_id,'_my_meta_facebook');
            else update_post_meta($post_id,'_my_meta_facebook',addslashes($new_data));
        }
        elseif (!is_null($new_data))
        {
            add_post_meta($post_id,'_my_meta_facebook', addslashes($new_data), TRUE);
        }
    
        return $post_id;

    I need to add this with slashes and double quotes intact:
    <meta name="fb:something" content="whatever"/>

    Thanks

  4. iansane
    Member
    Posted 2 years ago #

    Well I thought the problem was resolved but it's not.

    When saving meta data with a custom metabox add_post_meta() is stripping out double quotes and the closing '/' for the closing tag.

    I need to be able to save this:
    <meta name="fb:whatever" content="somecontent"/>

    How do I protect the string so add_post_meta() won't strip it?
    I tried addslashes() but that ends up causing an error somewhere and stoping it from writing anything to the database at all.

    here is my save code excluding the admin check part.

    $current_data = esc_textarea(get_post_meta($post_id, '_my_meta_facebook', TRUE)); 
    
        $new_data = $_POST['_my_meta_facebook'];
    
        //$new_data = addslashes($new_data);
        //my_meta_facebook_clean($new_data);
    
        if ($current_data)
        {
            if (is_null($new_data)) delete_post_meta($post_id,'_my_meta_facebook');
            else update_post_meta($post_id,'_my_meta_facebook',addslashes($new_data));
        }
        elseif (!is_null($new_data))
        {
            add_post_meta($post_id,'_my_meta_facebook', addslashes($new_data), TRUE);
        }
    
        return $post_id;

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic