最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Insert page content into another page with a changed variable

programmeradmin1浏览0评论

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

 <?php
$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!

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

 <?php
$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!

Share Improve this question asked Feb 27, 2020 at 10:06 Kristýna ŠulcováKristýna Šulcová 255 bronze badges 3
  • What you're trying to build is probably doable using the reusable blocks feature of the block editor – Tom J Nowell Commented Feb 27, 2020 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á Commented Feb 27, 2020 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 Commented Feb 27, 2020 at 11:55
Add a comment  | 

2 Answers 2

Reset to default 1

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.

Shortcodes

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.

发布评论

评论列表(0)

  1. 暂无评论