Okay
  Public Ticket #2231752
Add custom variables
Closed

Comments

  • Kim started the conversation

    Hi,

    First of all I want to congratulate you on this amazing plugin. It's really incredible what you created here.

    I am trying to figure out if I would be able to do the following thing and would be happy to buy the pro version if it is possible with it.

    I want to build an archive page that has the following layout:

    • Sub-category 1
      • Post 1
      • Post 2
      • Post 3
    • Sub-category 2
      • Post 4
      • Post 5 
      • Post 6
    • Sub-category 3
      • ...

    On top of that I want to pull custom fields and relationships from my pods custom post types.

    I don't think this is possible out of the box.

    So a potential solution could be for me to write PHP code that generates an array with the categories and posts. This code could be called through a filter from UE that adds my own variables to the twig instance and let's me use them in the element.

    I imagine it to be something like this:

    {% set categories = [{'name': 'sub-category 1', 'posts': ['Post 1', 'Post 2', 'Post 3']}, {'name': 'sub-category 2', 'posts': ['Post 4', 'Post 5', 'Post 6']}] %}
    <ul>
    {% for category in categories %}
      <li>{{ category['name'] }}</li>
      <ul>
    {% for post in category['posts'] %}
          <li>{{ post }}</li>
      {% endfor %}
      </ul>
    {% endfor %}
    </ul>

    So, all I need is to set the categories variable from my own code.

    Is this possible?

    I saw the pro version has an apply_filters() function, but I couldn't find any documentation.

  •   Max replied privately
  • Kim replied

    Hi, looks like I can't reply through email to the tickets, so you haven't been getting my messages.

    Thank you so much for writing the article.

    I purchased the pro version and tried it out already. At first it didn't work, but after a bit of digging I realized the article PHP example code is missing one line:

    add_filter( 'get_my_data', 'getMyData', 10, 3 );

    Once I added that it was working. It would probably help others if you updated the article.

    Another question...

    Now I have another request... Would you be able to expose the widget items as a variable?

    I wanted to build an accordion that includes the new FAQ schema.org markup, which looks like this:

    <script type="application/ld+json">
    {
      "@context": "https://schema.org",
      "@type": "FAQPage",
      "mainEntity": [
        {
          "@type": "Question",
          "name": "Some quesiton?",
          "acceptedAnswer": {
            "@type": "Answer",
            "text": "And here is the answer"
          }
        },
        {
          "@type": "Question",
          "name": "Some other question",
          "acceptedAnswer": {
            "@type": "Answer",
            "text": "And another answer"
          }
        }
      ]
    }
    </script>
    

    Looks easy enough, but unfortunately there is one difficulty... JSON requires a ',' (comma) between the elements, but the last element must not have a comma.

    I setup my code like this:

    s%2Fxq05d4b5l3aii89%2FScreen%2520Shot%25202019-12-06%2520at%252010.49.01.png

    And the output is:

    s%2Fitoev1oogik9wro%2FScreen%2520Shot%25202019-12-06%2520at%252010.50.20.png

    As you can see I don't have a way to hide the last comma when I use put_items(). It does work perfectly when I use a normal twig loop though.

    I suspect it's because put_items does the loop in PHP and so the loop variables are not included. Perhaps you could either include your own version of the loop variable in the item or even better if you could make the items accessible from twig itself.

  •   Max replied privately
  • Kim replied

    Hi Max,

    Thank you so much for your reply. That will work for this use case, but I have another widget I am trying to implement where I need the items to be twig variables...

    I am trying to implement Elementor's icon list, but with nested bullets.

    Widget HTML:

    <p>Fake list</p>
    {% set items = [
        {"text": "A bullet", "indent": 1},
        {"text": "An indented bullet", "indent": 2},
        {"text": "Another bullet", "indent": 2},
        {"text": "Back to the root", "indent": 1},
        {"text": "Another root", "indent": 1},
        {"text": "Sub-bullet", "indent": 2},
    ] %}
    {% set current_indent = items[0].indent %}
    {% for i in range(1, current_indent) %}
        <ul>
    {% endfor %}
    {% for item in items %}
          {% if item.indent > current_indent %}
              <ul>
        {% elseif item.indent < current_indent %}
            </ul>
        {% endif %}
        {% set current_indent = item.indent %}
        <li>{{ item.text|raw }}: {{ item.indent|raw }}</li>
    {% endfor %}
    {% for i in range(1, current_indent) %}
        </ul>
    {% endfor %}
    <hr>
    <p>Real List</p>
    {% set current_indent = 1 %}
    <ul>
    {{put_items()}}
    </ul>
    

    Item HTML:

    <li>{{item.title|raw}}: item indent: {{item.indent}} / current_indent: {{current_indent}}</li>
    {% set current_indent = item.indent %}

    And the output is:

    Fake list

    • A bullet: 1
      • An indented bullet: 2
      • Another bullet: 2
    • Back to the root: 1
    • Another root: 1
      • Sub-bullet: 2

    Real List

    • A bullet: item indent: 1 / current_indent:
    • An indented bullet: item indent: 2 / current_indent:
    • Another bullet: item indent: 2 / current_indent:
    • Back to the root: item indent: 1 / current_indent:
    • Another root: item indent: 1 / current_indent:
    • Sub-bullet: item indent: 2 / current_indent:

    So twig variables from the Widget HTML field are not available inside the item HTML. And even variables defined in the Item HTML field are not available on each iteration. This means I can't track which level I am on.

    I have attached my test widget in case you want to try it.

    Conclusion

    I understand that maybe your plugin is not meant for advanced uses-cases like this. 

    And I don't mean to be demanding. I am just in SO MUCH AWE of what you have created. It's really incredible and takes WP and Elementor to a completely new level.

    And if you could add a few more features it would make Elementor truly unlimited.

    In case you are open to implementing more advanced features, I'd also love to see these:

    • An "item.text | autop" filter that executes WP's autop function. I have used nl2br, but it creates issues with lists as it adds a <br> to every single line
    • A way to add my own twig filters from PHP
    • A way to add my own twig actions from PHP
  •   Max replied privately
  • Kim replied

    Hi Max,

    Thanks for your reply. I don't want to use twig variables, because I would have to hard-code the values in the widget. I do want to use the simplified items so that I can add items in Elementor from the UI.

    I included the code with the custom twig array to demonstrate what I want to achieve and why it's not possible with the current setup. 

    I need to be able to access the UI items as a twig variable instead of using put_items(). That's because I can't have a "global" variable that works between loop iterations when using 'Item HTML'.

    I understand that maybe your plugin is not meant for advanced uses-cases like this. 

    And I don't mean to be demanding. I am just in SO MUCH AWE of what you have created. It's really incredible and takes WP and Elementor to a completely new level.

    And if you could add a few more features it would make Elementor truly unlimited.

    In case you are open to implementing more advanced features, I'd also love to see these:

    • Expose items as a twig variable (most important one)
    • Add an "item.text | autop" filter that executes WP's autop function. I have used nl2br, but it creates issues with lists as it adds a <br> to every single line
    • A way to add my own twig filters from PHP
    • A way to add my own twig actions from PHP


  •   Max replied privately
  •   Kim replied privately
  •   Max replied privately
  •   Max replied privately
  • Kim replied

    Aweosme, thank you. I am really happy to hear! 

    Yeah pro version is fine as I already purchased it.

  •   Max replied privately
  • Kim replied

    awesome thanks. I can help testing the new version before you release it!

  •   Max replied privately
  • Rian replied

    I feel like the replies here may be helpful for something I'm trying to do, but they are all marked as private so I can't see them. For example, Kim refers to an article, but don't know where that article is.

  • Kim replied

    Hey Rian...


    I agere, the private replies are frustrating. I also searched in the previous tickets and thought there was useful stuff for me that I couldn't see.

    Either way, here's the article I referred to:

    https://unlimited-elements.helpscoutdocs.com/article/81-php-integration-get-data-from-php

    @max, thanks for the version. Sorry I missed the notification email for this and the new year started off really hectically. I think next week I should have time to test it all.

    Thanks for your hard work!

  • Rian replied

    Thanks Kim. For some reason the code examples link doesnt work for me, but I ended up figuring out what I needed by taking some hints from your posts and some additional sleuthing. From the screenshots in the article it seems like there may be some good info in those examples too, particularly for a beginner like me, if I can figure out how to access them. Anyway, the article confirms that I was on the right track I think, and for now I've figured out what I needed to do.

  •   Max replied privately