Viewing 15 replies - 1 through 15 (of 15 total)
  • Plugin Author DannyWeeks

    (@dannyweeks)

    Can you give an example of the code of what you want to store/display please. It might be possible to do it with the current functionality, if not it should be possible with a small function.

    Danny

    Thread Starter JMT007

    (@jmt007)

    Hi Danny,

    I need to display html images like <img src=”url” alt=”some_text”>

    https://www.w3schools.com/html/html_images.asp

    John

    Plugin Author DannyWeeks

    (@dannyweeks)

    ok no problem, do you need to access them through the php function or a shortcode?

    Thread Starter JMT007

    (@jmt007)

    The shortcode please. Thanks!

    Thread Starter JMT007

    (@jmt007)

    Hi Danny,

    I just wanted to follow-up and see if you had any luck with this?

    Thanks,
    John

    Plugin Author DannyWeeks

    (@dannyweeks)

    Sorry buddy I have been all over the place the last few weeks!

    Hopefully this solution will help!

    Create a new record with a reference of ‘images’ and add the below in the data field (just some example data!).

    {
        "hundred": {
            "url": "https://placehold.it/100",
            "alt": "one hundred"
        },
        "thousand": {
            "url": "https://placehold.it/1000",
            "alt": "one thousand"
        }
    }

    Add the below code to the bottom of your theme’s functions.php file.

    function get_image_element($atts) {
        $imageName = !empty($atts['name']) ? $atts['name'] : null;
        $url = '';
        $alt = '';
    
        if($imageName && function_exists('cwd_getThe')) {
            $img = cwd_getThe('images');
            if(!empty($img[$imageName])) {
                $url = $img[$imageName]['url'];
                $alt = $img[$imageName]['alt'];
            }
        }
    
        $format = "<img src=\"%s\" alt=\"%s\">";
        if(!empty($url)) {
            return sprintf($format, $url, $alt);
        }
    
        return null;
    }
    
    add_shortcode('image', 'get_image_element');

    You should be able to use the new shortcode to output the images.

    Using the test data you could use [image name="hundred"] or [image name="thousand"]

    Any problems mate just let me know and I’ll work through them with you!

    Thread Starter JMT007

    (@jmt007)

    Awesome, thanks! I’ll give this a shot and report back.

    Thread Starter JMT007

    (@jmt007)

    Hi Danny,

    The solution works great when I’m using the test data, but for some reason I can’t get it to work when I change the name and add my own image URL. I can’t for the life of me figure out why, please let me know if you have any ideas.

    Thanks!!

    Thread Starter JMT007

    (@jmt007)

    Ah…I see. I have to include all my images under the “images” record. Got it!

    Theoretically this should work just fine, the issue I’m having now is that I need to add a bunch of images under this record and many times after I add new data and click “Edit Record” nothing happens, the record stays the same…

    Also, would it be possible to manipulate the size of the images? i.e., width=”100″ height=”auto”

    Thanks again!

    Plugin Author DannyWeeks

    (@dannyweeks)

    Hey,

    It could be a syntax error in the json causing that issue try to validate it. If not try creating it as a new record and see if that works.

    Regarding the adding of width and height you can just have optional parameters on your shortcode, something like this:

    function get_image_element($atts) {
        $imageName = !empty($atts['name']) ? $atts['name'] : null;
        $url = '';
        $alt = '';
    
        if($imageName && function_exists('cwd_getThe')) {
            $img = cwd_getThe('images');
            if(!empty($img[$imageName])) {
                $url = $img[$imageName]['url'];
                $alt = $img[$imageName]['alt'];
            }
        }
    
        $optionalAttribues = '';
    
        if (!empty($atts['width'])) {
            $optionalAttribues .= sprintf('width="%s"', $atts['width']);
        }
        if (!empty($atts['height'])) {
            $optionalAttribues .= sprintf('height="%s"', $atts['height']);
        }
    
        $format = "<img src=\"%s\" alt=\"%s\" %s>";
        if(!empty($url)) {
            return sprintf($format, $url, $alt, $optionalAttribues);
        }
    
        return null;
    }
    
    add_shortcode('image', 'get_image_element');

    I haven’t tested it but it should be fine to use [image name=”hundred” width=”100″ height=”auto”] . You can leave off width and/or height and it will just act as it currently does.

    Thread Starter JMT007

    (@jmt007)

    Hi Danny,

    I’m not sure what the json is so I don’t know how to validate it.

    Here’s what I got so far…if I try and create a new “images” record it overrides the previous record and those shortcodes stop working. Also if I try to create an “images” record with more than two name/images it messes up and cuts off everything after the second name/image.

    Plugin Author DannyWeeks

    (@dannyweeks)

    Oh JMT007 you have found a bug!

    When I first created this plugin I didn’t intend for so much data to be stored in one record. The database table where all the records are stored can only contain 225 data characters which explains the cut off.

    We will have to tackle your problem in a different way until I can release a fix for this.

    OK, lets store each image in its own record with the reference prefixed with image.

    For example if we wanted to store an image called ten the reference would be image-ten and the data would be an array similar to have done previously:

    url=https://placehold.it/10
    alt=ten

    So the new record would look like this: https://gyazo.com/36692d3543a2c7e1f00abc701d41e117

    We need to replace the previous function we added to the functions.php file with this one

    function get_image_element($atts) {
        $imageName = !empty($atts['name']) ? $atts['name'] : null;
        $url = '';
        $alt = '';
    
        if($imageName && function_exists('cwd_getThe')) {
            $img = cwd_getThe('image-' . $imageName);
            if(!empty($img)) {
                $url = $img['url'];
                $alt = $img['alt'];
            }
        }
    
        $optionalAttribues = '';
    
        if (!empty($atts['width'])) {
            $optionalAttribues .= sprintf(' width="%s" ', $atts['width']);
        }
        if (!empty($atts['height'])) {
            $optionalAttribues .= sprintf(' height="%s" ', $atts['height']);
        }
    
        $format = "<img src=\"%s\" alt=\"%s\" %s>";
        if(!empty($url)) {
            return sprintf($format, $url, $alt, $optionalAttribues);
        }
    
        return null;
    }
    
    add_shortcode('image', 'get_image_element');

    The shortcode remains the same so with this example we could use [image name="ten"] or [image name=”ten” width=”100″]

    That should hopefully work well for you! Let me know how that goes ??

    Thread Starter JMT007

    (@jmt007)

    Hi Danny,

    Thanks for all your help. I just got back to my computer and haven’t tried this yet, but maybe I should explain a little more about what I’m trying to do.

    I’m actually only use two different image files, a “check” and an “X”. These are being used in comparison tables to compare a bunch of different products. So let’s say ABC company offers a particular service we’ll use a “check” in our tables, but lets say 6 months from now something changes and they no longer offer the same service we can change that data to and “x” across all our tables that display that particular feature. Hopefully that makes sense.

    With this in mind, will the current solution work? I’m thinking for our situation being able to name each record according to the “name” i.e. image-“name” might be the way to go. Let me know what you think.

    Thread Starter JMT007

    (@jmt007)

    Hi Danny,

    Did I lose you here? I understand this takes your time, please let me know if I can pay you for your work.

    Look forward to hearing from you.

    Plugin Author DannyWeeks

    (@dannyweeks)

    Hey,

    Sorry haven’t got back to you John. I support this plugin in my spare time and have had quite a packed schedule for a few weeks now. I couldn’t take any money from you as I no longer do client work. I assumed by only needed two images stored the solution I suggested should be appropriate. It does depend heavily on the implementation of the other features you have such as the tables. That said, I would suggest you take a look at a plugin called Advanced Custom Fields as it might prove very useful to you! A feature you may find useful is Repeater fields.

    Danny

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