admin管理员组

文章数量:1122832

I have limited budget and time for a new project. It's supposed to be a web application.But instead of opting for Symfony, I decided to use WordPress for following reasons:

  1. Adding blog feature requires no additional steps

  2. I want to use pre-existing WordPress plugins like SEO, hubspot without implementing them all over again.

  3. I also don't want to reimplement basic features like user management.

I have the WordPress installation ready now.

I don't need complex features like model, templating engine. But if the plugin support such feature (to be future proof), it's even nicer.

I have 10 different scripts in a special directory.

I want when user visit a page, it calls that PHP script and output the content on the page while using the theme

I have limited budget and time for a new project. It's supposed to be a web application.But instead of opting for Symfony, I decided to use WordPress for following reasons:

  1. Adding blog feature requires no additional steps

  2. I want to use pre-existing WordPress plugins like SEO, hubspot without implementing them all over again.

  3. I also don't want to reimplement basic features like user management.

I have the WordPress installation ready now.

I don't need complex features like model, templating engine. But if the plugin support such feature (to be future proof), it's even nicer.

I have 10 different scripts in a special directory.

I want when user visit a page, it calls that PHP script and output the content on the page while using the theme

Share Improve this question asked Aug 15, 2024 at 12:02 CodePandaCodePanda 101 6
  • have you looked at shortcodes/widgets/server rendered blocks/page templates – Tom J Nowell Commented Aug 15, 2024 at 12:35
  • @TomJNowell I have used shortcode plugins like wordpress.org/plugins/insert-php. But they are more like tools/library and things get very messy with them easily. But I am looking for a framework that's future proof if the project is successful. – CodePanda Commented Aug 15, 2024 at 12:51
  • people usually write their PHP code in plugins that are files in the filesystem, not into the admin area via insert php or code snippet type plugins. If you're looking for some sort of official WordPress MVC style framework where you shove all your code into objects implementing special WordPress classes and interfaces to handle requests then it's bad news as none of that exists, that's not how WP sites are built. Fundamentally what you're trying to do doesn't require a framework though. – Tom J Nowell Commented Aug 15, 2024 at 13:07
  • 1 Make templates for your 10 different scripts and then assign those templates to pages you make in WP, when people hit those pages your PHP runs. – Tony Djukic Commented Aug 15, 2024 at 14:03
  • @TomJNowell Yes. These PHP files are in separate folder and are a separate and I don't shove them inside code snippet plugins. I just want to connect these 2 projects in a clean way – CodePanda Commented Aug 15, 2024 at 14:50
 |  Show 1 more comment

1 Answer 1

Reset to default 1

It sounds like you are just trying to output specific content, which you have in non-WP PHP files right now, on a specific page.

If that's the case, the most straightforward way will be to create a custom WP plugin. All you need to do is add a folder inside your /wp-content/plugins/ folder with a PHP file that contains a simple comment:

<?php
/**
 * Plugin Name: Control Text
 * Version: 1.0.0
 */

Activate it in the wp-admin Plugins screen so you don't forget to do so and wonder why your script isn't working.

From there, it depends what your current scripts are doing and where you want to have them run. If you are generally wanting to output content inside the theme - like page content in between a header and a footer - you will probably use a hook like the_content.

For example, if one of your scripts currently contains the following (very simplified) logic:

<?php
$number = 1;
if ( $number > 0 ) {
     return 'My number is an integer.';
} else {
     return 'Not an integer.';
}

you can show that output on a particular WP Page by wrapping your code in a function inside that plugin.php file you just created, and then telling WP to run it when it outputs main content:

<?php
/**
 * Plugin Name: Control Text
 * Version: 1.0.0
 */

// Check whether a number is an integer and return the result.
function check_integer() {
    $number = 1;
    if ( $number > 0 ) {
         return 'My number is an integer.';
    } else {
         return 'Not an integer.';
    }
}

// Run the Check Integer function.
add_filter( 'the_content', 'check_integer' );

You probably want one additional step - identifying exactly where this content is output. Right now it would output on every page and post. If you create a Page at http://www.example.com/integer/ then the page slug is "integer" and you can update your function like so:

// Pass the existing $content into the function.
function check_integer( $content ) {
    // Only run on the 'integer' page.
    if ( is_page( 'integer' ) ) {
        $number = 1;
        if ( $number > 0 ) {
             return 'My number is an integer.';
        } else {
             return 'Not an integer.';
        }
    }

    // If we're not on the 'integer' page, return the normal content.
    return $content;
}

Obviously your logic may be much more complex, but basically, you are likely to do best by: creating a plugin, hooking it to the the_content filter, adding conditionals to target which pages you actually want to display content on, and putting your real code inside those conditionals.

本文标签: Framework plugin inside wordpress