Support » Plugin: Kirki Customizer Framework » Code control

  • Resolved ThemeAWESOME

    (@tsquez)


    Hi there,

    I was thinking of trying to use the code control in my theme and I got it into the customizer where I want and so now I have questions.

    1. I see you have this in the control example

    
    	'choices'     => array(
    		'language' => 'css',
    	),
    

    How can I add other languages like php or html?

    2. I did change the language to php and inserted <?php echo '<p>Hello World</p>' ;?> to see what would happen and I got the following msg from wp 4.9

    There are 2 errors which must be fixed before you can save. and there is a little line under the opening < and closing >

    So exactly how does one add multiple languages to the code control and how does php have to be inserted.

    Thanks

Viewing 15 replies - 1 through 15 (of 16 total)
  • Plugin Author Aristeides Stathopoulos

    (@aristath)

    Right now the code control has a few issues, v3.0.18 will be dedicated to them. I’m sorry for the delay on this one… Your code is correct, but with the release of WP 4.9 many things changed and code fields have to be refactored to accommodate those changes.

    Hi there,

    Thanks for the info and I know you will get it all worked out.

    So, like I mentioned above, I would like to know how to do a few things how do I use another language like php with code control. As I mentioned above you have this:

    	'choices'     => array(
    		'language' => 'css',
    	),
    

    so would I just do something like this:

    
    	'choices'     => array(
    		'language' => 'php',
    	),
    

    Can I use multiple languages within one control? Something like:

    
    	'choices'     => array(
    		'language' => 'php, html, css',
    	),
    

    or something like this:

    
    	'choices'     => array(
    		'language' => 'php',
                    'language' => 'html',
                    'language' => 'css',
    	),
    

    I’d like to know about this stuff beforehand. I would go to your document site however all I see is:

    The documentation site is currently being rewritten. If you can't find what you're looking for please check the github issues and ask for assistance there until the documentation rewrite is complete.

    Plugin Author Aristeides Stathopoulos

    (@aristath)

    so would I just do something like this:

    ‘choices’ => array(
    ‘language’ => ‘php’,
    ),

    Yes, this is how you’ll be able to do it – as soon as I manage to get to the code controls πŸ˜€

    Can I use multiple languages within one control?

    No. But using php also allows adding HTML, CSS & JS so that covers your scenario πŸ˜‰
    Code added in <?php ?> tags will be evaluated as PHP, if there is markup outside PHP tags it will be treated as HTML, if inside <style></style> tags it’s treated as CSS, and if inside <script></script> tags it’s treated as JS – or rather will be once done.

    In the meantime, if you could test the 3.0.17 version and let me know if you find any issues I’d appreciate it… I can’t start working on the code controls (v3.0.18) until v3.0.17 is out!

    Cool, thanks again.

    I test your plugin everyday amigo as I update the repo locally every morning. So far nothing to report.

    I will def keep an eye out and try an break it…lol I have to, it’s a big part of my new theme πŸ˜‰

    Plugin Author Aristeides Stathopoulos

    (@aristath)

    Awesome, thank you!
    I don’t expect any issues, it looks pretty solid. But still, lots of internal changes so I’ll leave it a day or two just to be sure nobody reports something new. If all goes well, 3.0.17 will be released by Tuesday.

    That’s awesome! Keep going, you make this plugin greater with every release.

    Like I said earlier its a major part of my new theme, without Kirki it’s blah, with it, it’s ThemeAWESOME! 😎

    Plugin Author Aristeides Stathopoulos

    (@aristath)

    I’m sorry I couldn’t fit the code controls refactor in 3.0.18, more urgent issues came up and I had to release a couple updates without it.
    However I now have a fix in the develop branch on github, could you please test it and let me know if it works for you?

    Plugin Author Aristeides Stathopoulos

    (@aristath)

    Should be ok now in 3.0.21

    So I tried the dev version of kirki 3.0.23-dev and I added a code control

    TP_Primo_Kirki::add_field( 'totalpress_theme_customizer', array(
    	'type'        => 'code',
    	'settings'    => 'totalpress_code_setting',
    	'label'       => esc_attr__( 'Code Control', 'textdomain' ),
    	'description' => esc_attr__( 'Description', 'textdomain' ),
    	'section'     => 'tp_primo_post_typography_section',
    	'default'     => '',
    	'priority'    => 999,
    	'choices'     => array(
    		'language' => 'php',
    	),
    ) );

    code control shows up fine, then I added <?php echo get_theme_mod('totalpress_code_setting') ; ?>where I want the code to display and then I added<?php echo ‘<p>Hello World</p>’ ;?>` into the code control and this is what displays:

    Hello World
    ‘ ;?>

    If I inspect it this is waht is shown:

    <!--?php echo'<p-->
    "Hello World"
    <p></p>
    ';?>

    now if I remove the ;?> this is what displays:

    Hello World
    '

    and if I inspect it this is what shows:

    <!--?php echo'<p-->
    "Hello World"
    <p></p>
    "' "

    if I keep going and remove </p>' this is what displays:

    Hello World

    and when I inspect it i see this:

    <!--?php echo'<p-->
    "Hello World "

    so keep going and remove <p> this is what happens in the customizer preview:

    var _wpCustomizePreviewNavMenusExports = {"navMenuInstanceArgs":{"ce8060b1b8316f4bff87eefc38a17c6f":{"after":"","before":"","can_partial_refresh":true,"container":"div","container_class":"","container_id":"","depth":0,"echo":true,"fallback_cb":"","item_spacing":"preserve","items_wrap":"
    %3$s<\/ul>","link_after":"","link_before":"","menu":73,"menu_class":"menu","menu_id":"","theme_location":"","walker":"","args_hmac":"ce8060b1b8316f4bff87eefc38a17c6f"},"90c2799a853e748ccdb68fec6c544a4c":{"after":"","before":"","can_partial_refresh":false,"container":false,"container_class":"","container_id":"","depth":0,"echo":true,"fallback_cb":"","item_spacing":"preserve","items_wrap":"
    %3$s<\/ul>","link_after":"","link_before":"","menu":"","menu_class":"menu","menu_id":"","theme_location":"primary","walker":"","args_hmac":"90c2799a853e748ccdb68fec6c544a4c"},"39db82227a9b58d44ab4b4465288fdc8":{"after":"","before":"","can_partial_refresh":true,"container":"div","container_class":"","container_id":"","depth":0,"echo":true,"fallback_cb":"","item_spacing":"preserve","items_wrap":"
    %3$s<\/ul>","link_after":"","link_before":"","menu":112,"menu_class":"menu","menu_id":"","theme_location":"","walker":"","args_hmac":"39db82227a9b58d44ab4b4465288fdc8"}}};

    but nothing displays on the front end, but when you inspect it, this is what shows:

    <!--?php echo 'Hello World <div class="back-to-top"-->

    Just thought I would share everything that I am seeing with the code control.

    Can you give me any insight as to what I may be doing wrong? Can you let me know how to add php properly into the code control.

    Plugin Author Aristeides Stathopoulos

    (@aristath)

    That has nothing to do with Kirki or WordPress… it’s just the nature of PHP.

    You can’t execute code from inside a string unless you use eval(), and using eval() is strictly prohibited for security reasons. What you’re doing in your examples above is echo the string that contains the PHP. However it still remains a string no matter how you manipulate it.

    tbh I can’t understand why you want to allow users to add PHP… It’s completely insecure and can lead to disaster. If a user does something wrong their site will simply fail with a fatal error and the only way to recover would be to manually edit the database and delete the theme-mod.

    Why not simply use JS?? Nothing runs server-side and it’s just as flexible as PHP…
    Why do you want to add a PHP field where users can enter code that will be executed?

    Thanks for the scolding…I appreciate it.

    I was just trying to show you what I was seeing. I do not know how to insert php into your code control so i was doing it the way I thought would work.

    I asked you on numerous occasions for an example of how to insert PHP using your code control and never got a response. All I got was “this has been fixed in version blah blah blah”

    I even asked you in my previous response yesterday:

    Can you give me any insight as to what I may be doing wrong? Can you let me know how to add php properly into the code control.

    All I got was you saying it has nothing to do with WordPress and basically, from my perspective, called me an idiot or made me feel like one.

    In response to your question

    “Why do you want to add a PHP field where users can enter code that will be executed?”

    I suppose you have never used a theme that uses hooks or allows shortcodes?

    For example the Genesis theme. Heres the plugin that you can use https://wordpress.org/plugins/genesis-simple-hooks/ – as you can see it states :

    This plugin creates a new Genesis settings page that allows you to insert code (HTML, Shortcodes, and PHP), and attach it to any of the 50+ action hooks throughout the Genesis Theme Framework, from StudioPress.

    This is what I was shooting for with your code control, to see if it was possible to use for doing the same thing, inserting code (HTML, Shortcodes, and PHP), and attach it to any of the action hooks throughout the theme I am currently working on.

    But I guess that would be a no according to you.

    Thanks again.

    Plugin Author Aristeides Stathopoulos

    (@aristath)

    I’m terribly sorry if my previous reply seemed like “scolding”, I certainly didn’t mean it that way. English is not my native language and sometimes the tone of a message doesn’t translate that well from a language like Greek.
    My sincerest apologies.

    What was fixed was the error you were getting when adding PHP code in the control.

    I have used various themes that use hooks, shortcodes etc, but I’ve never seen the plugin you linked to before so I don’t know how they accomplished what you’re describing. I’ll download it and take a look at it later today to see how they do it and if they have a good solution to this problem.

    Generally speaking there are 2 ways to execute code that has been stored in a string:

    1. Use eval() to evaluate the code and execute it. The problem with this method is that if there is an error somewhere in the PHP code, it will result in a PHP Fatal error and crash everything.
    2. β€ŽStore the code in an actual PHP file using the WP_Filesystem class in a temp file, then try to include the file, check and catch any errors, if there is an error show it to the user and early exit, if there is no error save the temp file to its final location and include it. This is actually what the theme editor does in wp-core…

    Both methods have their pros and cons… The 1st method is obviously the simplest solution but also a bit dangerous and forbidden in themes hosted on w.org, the 2nd solution is safer but way too complicated to implement in a theme unless someone spends dozens of hours to write an implementation from scratch which will be a relatively large task.

    Adding HTML and shortcodes can be done using the HTML mode. Executing user-entered PHP however has to be handled with extreme care and caution to avoid breaking user sites in case the user for example forgets to close a PHP tag, has a typo in a function-name or something else equally trivial.

    My previous reply was simply meant to highlight the dangers of using PHP like that, I apologize again if it came out wrong.

    No worries amigo, I understand completely. Things get lost in translation and stuff can go down hill from there.

    However I love your plugin and what it does and how it does it. It’s been a lifesaver and I couldn’t ask for more.

    I hope you can look at that plugin I linked to above and find a solution to do something similar in your plugin. If you could do that…that would be the best thing since sliced bread πŸ˜‰

    Plugin Author Aristeides Stathopoulos

    (@aristath)

    Just an update here, I just checked the plugin you linked to in a previous reply and it uses the eval() function to execute code: https://github.com/copyblogger/genesis-simple-hooks/blob/99c1ae87f4ad2f1e01840520990e2846a87a3990/genesis-simple-hooks.php#L186

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Code control’ is closed to new replies.