admin管理员组文章数量:1391955
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
2 Answers
Reset to default 1Assuming 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.
本文标签: phpInsert page content into another page with a changed variable
版权声明:本文标题:php - Insert page content into another page with a changed variable 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744705956a2620845.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论