Skip to content

Commit 7763d3f

Browse files
authored
Merge pull request #25 from INN/21-add-pym_id-attribute
Add a new attribute id="" for setting an explicit id on the iframe.
2 parents bf96855 + 3979462 commit 7763d3f

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
## All options:
1111

1212
```
13-
[pym src="" pymsrc="" pymoptions=""]
13+
[pym src="" pymsrc="" pymoptions="" id="" ]
1414
```
1515

1616
`src` is the URL of the page that is to be embedded.
@@ -28,3 +28,11 @@ To do the same thing with this Pym shortcode, you would write:
2828
```
2929
[pym src="child.html" pymoptions=" xdomain: '\\*\.npr\.org' "]
3030
```
31+
32+
`id` is optional; this should be a valid HTML element ID name. It will be used as the ID of your `pymParent` iframe on the parent page. You would want to use this if, for example, [your embedded page contained navigation to another page, requiring the second page to know the pymParent element ID](https://github.com/INN/pym-shortcode/issues/20).
33+
34+
For example, the shortcode `[pym src="http://blog.apps.npr.org/pym.js/examples/graphic/child.html" id="extremely_specific_id"]` results in the following output:
35+
36+
```html
37+
<div id="extremely_specific_example"></div><script src="http://example.org/wp-content/plugins/pym-shortcode/js/pym.v1.min.js"></script><script>var pym_0 = new pym.Parent('extremely_specific_example', 'http://blog.apps.npr.org/pym.js/examples/graphic/child.html', {})</script>
38+
```

pym-shortcode.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,17 @@ function pym_shortcode( $atts, $context, $tag ) {
3030

3131
$pymsrc = empty( $atts['pymsrc'] ) ? plugins_url( '/js/pym.v1.min.js', __FILE__ ) : $atts['pymsrc'];
3232
$pymoptions = empty( $atts['pymoptions'] ) ? '' : $atts['pymoptions'];
33+
$id = empty( $atts['id'] ) ? '' : esc_attr( $atts['id'] );
34+
$actual_id = empty( $id ) ? 'pym_' . $pym_id : $id;
3335

3436
$src = $atts['src'];
3537

3638
ob_start();
3739

38-
echo '<div id="pym_' . $pym_id . '"></div>';
40+
printf(
41+
'<div id="%1$s"></div>',
42+
$actual_id
43+
);
3944

4045
// If this is the first one on the page, output the pym src
4146
// or if the pymsrc is set, output that.
@@ -51,7 +56,7 @@ function pym_shortcode( $atts, $context, $tag ) {
5156
echo sprintf(
5257
'var pym_%1$s = new pym.Parent(\'%2$s\', \'%3$s\', {%4$s})',
5358
$pym_id,
54-
"pym_$pym_id",
59+
$actual_id,
5560
$src,
5661
$pymoptions
5762
);

readme.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ Mobile view of the WordPress post with the NPR embed using Pym Shortcode:
4848

4949
* Update to pym.js version 1.2.2: https://github.com/nprapps/pym.js/releases/tag/v1.2.2 (Changelog at https://github.com/nprapps/pym.js/blob/master/CHANGELOG )
5050
* (we skipped pym.js version 1.2.1: https://github.com/nprapps/pym.js/releases/tag/v1.2.1 )
51+
* Add `id=""` attribute to allow setting custom IDs on embeds. [#21](https://github.com/INN/pym-shortcode/issues/21)
5152

5253
= 1.2.0.2 =
5354

54-
* Fix encoding error on pym.v1.min.js, [thanks to lchheng](https://github.com/INN/pym-shortcode/pull/18
55+
* Fix encoding error on pym.v1.min.js, [thanks to lchheng](https://github.com/INN/pym-shortcode/pull/18)
5556

5657
= 1.2.0.1 =
5758

0 commit comments

Comments
 (0)