2

I created an angular single-page web app for my customer.

Now they need to integrate the app into a page of their (wordpress) website.

EDIT: in other words they want the app inside an existing wordpress page

what's the best approach?

I tried iframe but it does not work: no resize on app content change and problems with modals.

Thanks

2 Answers 2

2

If you need to insert it in an existing page with an already done template you can create a shortcode and a plugin:

create a folder like "your_spa" in the plugin folder of wordpress (/wp-content/plugins/)

create a php file named your_spa.php inside the new generated folder

put this inside the file "your_spa.php"

<?php
/*
Plugin Name: your spa plugin
Description: description
*/

function your_spa_code(){ ?>

<!-- put here your code (this will be inside the body of the page) -->

<?php }

add_shortcode( 'yourspa', 'your_spa_code' );
?>

take care of the links/resources urls (js, json, css): place them anywhere you want them, but remember the path (like in html path => url)

remember to let the apache user read the files (file permissions)

activate the plugin "your spa plugin" inside the wordpress dashboard

use [yourspa] inside a blogpost/page as a shortcut

And now you have created a plugin and a shortcode!

PS: remember that your code will be surrounded by the code of the existing page

It's a little dirty but it's the easies solution.

Sign up to request clarification or add additional context in comments.

1 Comment

I need my app included in an existing wordpress page
1

I had some success with simply rendering Angular's bootstrap code, the <app-root> and <script> tags, just as they are served from a standalone an Angular deployment.

I just added the following HTML in a post, using the HTML editor.

Of course, I had to fiddle the JS script source URLs. Rendering all this HTML could be done with a Wordpress shortcode and plugin that asks for a URL path to the JS files.

<app-root></app-root>
<script type="text/javascript" src="http://localhost:4200/inline.bundle.js"></script>
<script type="text/javascript" src="http://localhost:4200/polyfills.bundle.js"></script>
<script type="text/javascript" src="http://localhost:4200/styles.bundle.js"></script>
<script type="text/javascript" src="http://localhost:4200/scripts.bundle.js"></script>
<script type="text/javascript" src="http://localhost:4200/vendor.bundle.js"></script>
<script type="text/javascript" src="http://localhost:4200/main.bundle.js"></script>

This worked poorly with the Divi theme and a 'code' module, totally screwed up the page. Attempts to use the offical Wordpress mechanism for including JS scripts (wp_enqueue_script) failed since the <app-root> tag can't be found when the scripts load.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.