• Resolved pgrafix

    (@pgrafix)


    I am butting my head against the wall attempting to decode base64-encoded content at export or import. The site uses WP Bakery’s Virtual Composer as it’s page builder and it wraps encoded raw html between a shortcode designator: [vc_raw_html]base64-encoded html here[/vc_raw_html]. FYI, there other VC shotcodes in the content as well, but I am not having issues with modifying these via preg_replace.

    Here is the function I wrote to a) match the text between the two “tags”; b) decode the matching string; and c) replace the encoded string with the decoded string:

    $rhtml = preg_match("(?<=\[vc_raw_html]).*?(?=\[\/vc_raw_html])");
    $rstr = base64_decode($rhtml);
    function convert_raw_html($ID){
    	if ( $content = get_post_field( 'post_content', $ID ) ) {
    	$content = preg_replace($rhtml, $rstr, $content);
    		return htmlentities( $content );
    }
    }

    The preg_match statement does select the encoded string between the tags according to testing on regex101.com against real data, but the code above totally deletes all content when trying to implement during an export (and import for that matter). I have also tested a variant where the preg_match and decode parameters are inline in the preg_replace.

    If it were just a few records then I would handle this outside of the export/import process, but it is well over 200 records.

    Any help would be greatly appreciated.

    • This topic was modified 3 years, 11 months ago by pgrafix.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author WP All Import

    (@wpallimport)

    Hi @pgrafix,

    Here’s an example of how you might need to modify your function to make it work in the export:

    function convert_raw_html($content){
    	preg_match_all("/(?<=\[vc_raw_html]).*?(?=\[\/vc_raw_html])/", $content, $matches );
    	if ( ! empty( $matches ) ){
    		foreach ( $matches[0] as $match ) {
    			$rstr = base64_decode( $match );
    			$content = str_replace( $match[0], htmlentities( $rstr ), $content );
    		}
    	}
    	return $content;
    }

    This assumes you’re passing the content element to the function during the export: https://www.wpallimport.com/documentation/export/pass-data-through-php-functions/.

    Please keep in mind that this is only an example and it may need to be modified, which we won’t be able to help with. If you still have trouble, though, feel free to follow up here and we’ll keep the thread open in case anyone else is able to help.

    • This reply was modified 3 years, 10 months ago by WP All Import. Reason: Modified for export instead of import
    Plugin Author WP All Import

    (@wpallimport)

    Hi @pgrafix,

    I’m going to mark this as resolved since it’s been a while. Feel free to follow up in this thread if you still have questions.

    Anyone else with questions, please open a new topic.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Decoding base64 endoded text’ is closed to new replies.