Forum Replies Created

Viewing 10 replies - 16 through 25 (of 25 total)
  • Thread Starter chz516

    (@chz516)

    Ok, if anyone is interested, I have narrowed it down to a problem with: /wordpress/wp-content/themes/twentyfifteen/js/functions.js

    I guess that makes sense–a functions file specific to the theme.

    Thread Starter chz516

    (@chz516)

    So then I would expect a complete echo to work as well, but it doesnt:

    echo '<style type="text/css">.chpop {color:blue;}</style>';
    echo '<div class="chpop" id="chPop">bbbbbbbbbbbbbbbbb</div>';
    echo '<span onClick="document.getElementById(\'chPop\').style.color=\'green\';">clickme</span>';

    For the Ids, I am actually using: $this->get_field_id($fld);

    I am assuming this is the correct Id, unique even with multiple widgets (where $this is the instance of the widget itself?)

    Thanks for all of your help so far. Like I mentioned, I am new to PHP, so this is very helpful. My widget was coded in javascript an I’m really just wrapping the PHP around it. I only have two real issues remaining that cant get past. This one, and a strange behavior in widget itself (which I will be posting about as well because I just cant make sense out of it).

    Thread Starter chz516

    (@chz516)

    Ohhh…. I think I get it.

    Yes it is set up as a class (because that was what the code sample I found had)

    This clears up so much for me now. Although, now I wonder why I see $instance is passed around to various functions. Shoulnt it be accessible everywhere, just like $this?

    Thread Starter chz516

    (@chz516)

    Ok, one last thing I cant make sense of. In function form, I have many inputs to be created. They are all type=text so I figured I could call a function to build each form field. Something like:

    createTextbox('Field One');
    createTextbox('Field Two');
    etc;
    
    function createTextbox($fldname){
       ...use fldname to create the textbox
    }

    Now code as it is now, uses the below to build a form field. So, I just need to move this into a function…

    function form($instance) {	
    		extract(array_map('esc_attr', $instance));
    <?php/*<p><label for="<?php echo $this->get_field_id('align'); ?>"><?php _e('align:'); ?></label><input class="widefat" id="<?php echo $this->get_field_id('align'); ?>" name="<?php echo $this->get_field_name('align'); ?>" type="text" value="<?php echo $align; ?>" /></p> */?>

    But I cant figure out what $this means in this code. I know (think) it represents
    the current object, but there is no current object here that I can see. If I were looping through $instance, it would make sense. But what is $this here? I can see that I need $this to be sent into the function… but just dont get it.

    Thread Starter chz516

    (@chz516)

    Ok, the code works exactly. The array_map to set the instances in each function and the foreach to loop through for a bunch of echo statements.

    My widget is pretty simple PHP-wise. I developed in javascript, not intended originally for WP and decided to port it over. So really the PHP is just a wrapper for the js. Still, because of all the options, the code was getting bulky. It is now really small and condensed! So somebody here is a genius. Either me or you guys ??

    Thanks so much. What a huge help. I am new to PHP (I use ASP/vbscript) so was struggling with the syntax and structure a bit.

    • This reply was modified 6 years, 8 months ago by chz516.
    • This reply was modified 6 years, 8 months ago by chz516.
    Thread Starter chz516

    (@chz516)

    I misread and didnt didnt see it labeled as an array in the docs (but it is) which is why it seemed strange that it was being referenced as an array.

    I guess what I was hoping to find was a way to access the variable name. This way you could loop through them and condense all the code instead of explicitly refering to each key. It doesnt look like extract() will do that, but will get the values. As for array_map()… I cant even understand it! Im new to PHP so was hoping to find a way to reference the variable names of the array, but i guess no languages really do that. I see the variable variables, but many of the comments i have seen mention how quickly it begins to confuse the code.

    Maybe just hardcoding the long way makes more sense in this case? Although I cant be the first one to use a lot of options, and Im sure many use far more. What woulb be best practice to optimize in this case?

    Thread Starter chz516

    (@chz516)

    Sorry, I set my zIndex even higher and it does cover the menu now. And, really, that is a fine solution. I cant see a way to delete this post but dont want to make anyone waste their time on it.

    Thread Starter chz516

    (@chz516)

    Yes, the widget/plugin relationship is pretty clear after playing around a bit. So that makes complete sense.

    The simplest shortcut is to simply output your JS on a page somewhere. It can even be within post content as long as it’s within <script> tags and there are no empty lines.

    This seems to be the sticking point for me. Using a standard example it was pretty easy to add my widget. I can go to my page and add it but the only problem is it is a fairly wide widget (it is actually an iFrame that pulls part of a page from another site) so while a wide sidebar is ok, often, it will make more sense to have the widget inline with the main body of the HTML.

    I in order to add it to the main body, and not in a widget area, it requires a div and a script (unless being in WP requires a different approach, but i think you are saying it does not). If you were coding an actual HTML file, you would paste the div and script into the body wherever you want it to show up. But in a WP page, it seems you would simply, as you mentioned, put the script into the area where you write your blog content. But when I do that, it shows up as text… the same way code does in this forum–not resolved but as if it were in a PRE tag.

    Am I misunderstanding?

    Is adding inline div and script to the main body just a matter of adding it as part of the main blog text? And is that the correct way to add this to the main part (as it seems widgets can only be added to widget areas.)

    Thread Starter chz516

    (@chz516)

    Ok, just creating an example helps to make sense of this all (as expected) but one thing that has been driving me crazy for hours, how do I add an inline script?

    I have enqued the header and bottom scripts, but the middle script, the inline one, needs to be in the body–at the location where the widget is placed.

      <script>
        contentId="123";
        getContent();
      </script>

    I figured it is is “wp_add_inline_script” but that only seems to add lines to an existing registered script (such as header or bottom)

    The header script loads all the functions, now they just need to be called with some parameters.

    If I simply, wanted to add this to the widget:
    <script>alert('hello');</script>

    How would that be done? Does a js file need to be created and registered for any inline js?

    • This reply was modified 6 years, 9 months ago by chz516.
    Thread Starter chz516

    (@chz516)

    Well, its not that I think a plugin would be a poor choice so much as I think I am still a little confused between a plugin and widget.

    My understanding is that a plugin adds to the functionality of the site at the site level. And that a widget is more localized.

    So, if I am correct, then my assumption is that I need a plugin to be able to add my widgets. The plugin would be responsible for putting the js scripts at the top and bottom of the page and the widget would be the code that calls the js functions.

    But maybe I am still confused (and I dont even want to think about themes–that is an entirely new level of confusion ??

    I guess what I wonder is, is a WP plugin/widget needed. In other words, if I gave you the snippets of code above (the original post) would you easily be able to add them to your WP page? I know that if a site is not WP, its very easy. Just open the HTML page, paste the code where you need it, and all set. If it is just as easy in a WP site, is the widget needed.

    And, I agree that a boilerplate setup would probably work in my case because it is fully coded already. All the js functions, css, etc are all ready to go. Its really just a matter of leveraging that code and adhering to the WP guidelines.

    So if a WP plugin/widget is the way to go I dont think it would be hard to do, Im just still a bit confused as to WHAT to do.

Viewing 10 replies - 16 through 25 (of 25 total)