I'm trying to use this simple code to insert page (1) content into another page (2):

$id = 216;
$p = get_page($id);
echo apply_filters('the_content', $p->post_content);

So far so good. The inserted page (1), though, is some html code that at one point contains

value="<?php echo $url?>"

Is there any way I can edit the value of that variable in the code above, i.e. in page (2)?

Thank you!

  • What you're trying to build is probably doable using the reusable blocks feature of the block editor – Tom J Nowell Feb 27 '20 at 10:27
  • @tom that way I can only insert the page content as is and I don't want to do that..? – Kristýna Šulcová Feb 27 '20 at 10:36
  • Can you be more clear about what you're wanting to do then? What are you trying to use this to implement? What's the tag that the value attribute is a part of? And how is it you're able to put raw PHP code inside post content? ( if you're using a plugin that lets you put PHP directly into post content, this is a massive security flaw of the most extreme kind, and not how WP development should be done, if you need to run PHP code inside a post, you're supposed to use shortcodes, also, the use of these kinds of plugins is super important to know about, you must mention this ) – Tom J Nowell Feb 27 '20 at 11:55

Assuming you want to replace it with some static value (well, you can always change this anyway), some kind of find and replace snippet like this can do the trick:

$content = apply_filters( 'the_content', $post->post_content);
// set the replacement string value
$replace = "REPLACEMENT";
// make sure the quotes used are exact
$find_start = "<param name='filter' " . 'value="';
$find_end = '"';
// find the (end of) search string start position
$pos = strpos( $content, $find_start) + strlen( $find_start );
// get the content before (end of) search string
$before = substr( $content, 0, $pos );
// get content after search string
$after = substr( $content, $pos, strlen( $content ) );
// strip the after-content before end quote
$pos2 = strpos( $after, $find_end );
$after = substr( $after, $pos2, strlen( $after) );
// recombine content with replacement value
$content = $before . $replace . $after );
return $content;

There's a fundamental problem in your code, and one that suggests you're doing something very dangerous in your code.

Given this code:

$id = 216;
$p = get_page($id);
echo apply_filters('the_content', $p->post_content);

I would expect this to be running from a template file, however, the question claims that the post content contains this:

value="<?php echo $url?>"

This is not possible in normal WordPress, so you must be using a plugin that lets you put PHP tags inside post content. This is the source of all your problems. Because of this:

  • Most WordPress knowledge is unusable to you
  • The vast majority of tutorials will not work as they assume you're in a template file
  • Large numbers of solutions simply won't work correctly

Using plugins that let you embed PHP inside a post is extremely unusual, and super important to know. A person unaware of this will suggest things that will not work.

This is one of those situations where the answer you seek is not possible using a plugin that lets you use PHP inside a posts content.

Additionally, it's incredibly dangerous from a security point of view. But there is a much easier way that uses standard WordPress practices

Thus, I do not believe it is possible to solve your problem as long as you persist with the plugin allowing you to embed PHP inside a post. Never use something that lets you store PHP code in the database and run it from the database.


If you need to embed PHP logic inside a post/page, create a shortcode.

This way, your page can be something like this:

My page!

[kristynas_form foo="bar"]

In a plugin or your themes functions.php you can use something like this:

add_shortcode( 'kristynas_form', function( array $attributes )  {
    $foo = $attributes['bar'];
    return 'form HTML';

When the page gets rendered the shortcode gets swapped out. This is how you're supposed to implement things like iframes, forms, and other complex logic that appears inline.

  • Thank you, that's helpful, I'm a complete beginner so I definitely appreciate this advice. As to what I'm trying to do (insert page1 content into other pages, every time with a different variable value inside page1 html code) I am as lost as I was before... – Kristýna Šulcová Feb 27 '20 at 12:30
  • By using a PHP embedding plugin you've been lead down the wrong path, and have run into one of the dead ends :( If you embed the post into another post using shortcodes, then you could code a shortcode such as [embed_page id="216"], and then use different shortcodes inside that page 216 for your form. Then you would have something that you can ask about, and that most WP developers would be able to help with. As long as you're using a PHP embed plugin though, in order to help, we'd need to see all the code and even then it would be extremely difficult to solve for what should be a simple fix – Tom J Nowell Feb 27 '20 at 12:35
  • Essentially, these plugins let you do the super basic stuff easy, but everything else is like a game with the difficulty set to ultra hardcore – Tom J Nowell Feb 27 '20 at 12:37
  • Ok so I have this: add_shortcode( 'insert_k_post', function ($id) { $p = get_post($id['id']); echo apply_filters('the_content', $p->post_content); }); and it's working. The post I'm trying to insert contains this: <param name='filter' value="" /> where the last-mentioned value (now null) needs changing basically every time I use the shortcode. Is that doable? Could you at least nudge me towards the solution, @Tom J Nowell? Many thanks... – Kristýna Šulcová Feb 27 '20 at 15:03
  • It's extremely difficult ( perhaps impossible ) without knowing anything about what you're doing, I'd be making random guesses as I don't know what the form is, what the page does, what the value is meant to represent, why it needs to change, what it needs to change to, where the value comes from, etc, etc, But also, it's not what you originally asked. Instead, I think you should ask how to build it instead of how to fix it. Also, keep in mind that you're supposed to return not echo in a shortcode – Tom J Nowell Feb 27 '20 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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