Integrating WordPress Into PHP Scripts

WordHerd | 10 月 3, 2015 | 2分钟阅读
分享这篇文章

Overview

Right now, I had to make use of a customized PHP script that I created some time in the past in a brand new WordPress software. As an alternative of creating a WordPress plugin to deal with this present code, I select as an alternative to make use of WordPress instantly inside my script.

Utilizing WordPress inside your script is a superb choice if in case you have numerous customized PHP code.

When you might have a big PHP code base out of your earlier improvement efforts. There are sometimes different libraries inside your code, similar to Symfony, Zend Framework, Pear, and so forth, that you do not need to take aside. You could have working code and also you need it to remain collectively.

In my case, I had created a type for knowledge assortment for a shopper. However this was no odd type. It had over 100 type variables. They have been made up of textual content enter, radio packing containers, checkboxes, and textarea’s. I needed to make use of this manner immediately inside my WordPress software. Plus I needed the mixing to straightforward.

The answer was truly fairly easy.

Present PHP Code

For instance, assume that is my present PHP code (it’s a easy script as an example this level).

</p> <form name="datacollection" method="post" action="index.php" data-trp-original-action="index.php"> <enter sort="textual content" identify="first_name" worth="<php echo $_POST['first_name'];?>&#8220;><br /><input type="submit" value="Submit Data"><br /> <input type="hidden" name="trp-form-language" value="zh"/></form> <p>

However the issue is, that’s solely the shape to gather the info. What about the feel and appear of the prevailing website. How can I get the above code to appear to be the identical template format of my WordPress software.

For instance, one answer is so as to add the uncooked HTML. However that’s each time consuming, and it isn’t dynamic. If the WordPress template modifications, I’ve to go in right here and manually change it once more. Not one thing I need to do.

<head><br /> <title>My PHP Type Web page&lt;br /&gt; </head><br /> <body></p> <form name="datacollection" method="post" action="index.php" data-trp-original-action="index.php"> <enter sort="textual content" identify="first_name" worth="<php echo $_POST['first_name'];?>&#8220;><br /><input type="submit" value="Submit Data"><br /> <input type="hidden" name="trp-form-language" value="zh"/></form> <p></body><br /> </html>

However that would not precisely work. I must match the navigation, embrace the fashion sheets, and ensure the HTML matched precisely the feel and appear of my principal web site.

The Higher Answer

Instantiate WordPress, and use the inbuilt features inside your code.

<php ></p> <form name="datacollection" method="post" action="index.php" data-trp-original-action="index.php"> <enter sort="textual content" identify="first_name" worth="<php echo $_POST['first_name'];?>&#8220;><br /><input type="submit" value="Submit Data"><br /> <input type="hidden" name="trp-form-language" value="zh"/></form> <php >

Do you see how straightforward that was? WordPress is instantiated proper in my web page. Then I instruct the web page to name the header and footer features of WordPress.

The header and footer features present the whole lot I want for the template of my WordPress website. It mechanically populates the HTML, CSS, and any Javascript up on prime, and on the underside it closes any tags that have been open.

结论

Let PHP do the work. There isn’t any have to duplicate the WordPress code in pure HTML when you possibly can have it generated mechanically by WordPress itself.

The additional advantage is that if the primary WordPress website format ever modifications, the template wrapping your customized PHP code will mirror these modifications instantly.

阅读完整文章 >

阅读更多来自这位作者的作品

关键词:WordPress

准备好迁移您的网站了吗?

首先进行免费的无义务网站评估。我们将对您的项目进行评估,推荐合适的平台,并为您提供明确的报价。

保证零数据丢失
完整 SEO 保留与重定向映射
准时且按预算交付
专业化的平台专家,而非泛泛的专家

请告诉我们您的网站信息

"*"*” 表示必填字段

此字段用于验证目的,不应更改。
姓名*