0

I am trying to prepend my a php template in wordpress into my wordpress login form.

They only way I can edit markup in the body of wordpess login is to prepend via jquery.

This is my php template load: <?php load_template_part('menu'); ?>

This is my load template filter that filters get_template_part(); ...

// LOAD TEMPLATE PART
function load_template_part($template_name, $part_name=null) {
    ob_start();
    get_template_part($template_name, $part_name);
    $var = preg_replace('/\s\s+/', "", ob_get_contents());
    ob_end_clean();
    return $var;
}

In this filter I am removing all spaces between elements using this... preg_replace('/\s\s+/', "", ob_get_contents());


I am then trying to append this template part using this jquery...

var menu = '<?php echo load_template_part('menu'); ?>';

$("body").prepend(menu);

But it gets a javascript syntax error because of line breaks in my php load_template_part string.

Can any one please help me either remove line breaks from the php string or suggest another method of getting my php block inside my body safely.

Outputted source: https://gist.github.com/jcranny/5363379

Thanks Josh

6
  • possible duplicate of stackoverflow.com/q/5258543/901048 Commented Apr 11, 2013 at 13:24
  • Nope there is 100% not single quotes or scripts. the string is clean. Apart from line breaks Commented Apr 11, 2013 at 13:24
  • @Blazemonger my string has no single quotes. The string is clean of anything that will interrupt my javascript string. Commented Apr 11, 2013 at 13:25
  • Please show us exactly what IS being output by your PHP function. Commented Apr 11, 2013 at 13:26
  • Can you post a screenshot of the Source that actually gets rendered? Commented Apr 11, 2013 at 13:27

2 Answers 2

2

What if you simply replace all the linebreaks with spaces?

var menu = '<?php echo preg_replace('/[\r\n]+/',' ',load_template_part('menu')); ?>';

This may work more reliably, although I haven't tested it (borrowed from this answer):

var menu = '<?php echo json_encode(load_template_part('menu')); ?>';
Sign up to request clarification or add additional context in comments.

5 Comments

I don't think line breaks are this issue, I think the html returned by load_template_part contains literal single quotes. Since the PHP is parsed before the page is loaded, load_template_part() would end up simply being a literal string assigned to the "menu" variable.
@Drewdiddy611 OP was very specific that line breaks were his issue, although single quotes SHOULD also be a concern.
Just noticed the comment mentioning there were not single quotes returned.. My apologies :)
@Blazemonger Thank you - this fixed the issue! Now my js string is one long line with no js OP errors from line breaks. Thank you.
Don't forget this question if and when you need to escape single quotes in that line.
0

I've run into this issue before, you're going to have problems if that "template" contains single quotes. The work around I've used is to base64_encode the html that is returned from

load_template_part()

Then grab the base64_decode libraries from phpjs.org. This will ensure the string doesn't contain illegal characters that could cause a JS syntax error.

For example:

// With Base64 object from phpjs.org
var menu = Base64.decode('<?php echo base64_encode(load_template_part('menu')); ?>');

Give that a shot, always works like a charm for me.

3 Comments

I've just gone to phpjs.org but I cant seem to find the js library, is that what it is?
Thanks for you awnswer will still check this out

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.