{"id":1662,"date":"2018-08-02T19:50:02","date_gmt":"2018-08-03T03:50:02","guid":{"rendered":"https:\/\/www.waveplayer.info\/?p=1662"},"modified":"2020-06-01T00:37:56","modified_gmt":"2020-06-01T07:37:56","slug":"embedding-waveplayer","status":"publish","type":"post","link":"https:\/\/www.waveplayer.info\/docs\/embedding-waveplayer\/","title":{"rendered":"Embedding WavePlayer"},"content":{"rendered":"\n<p>Starting from version 2.3.1, it is now possible to embed WavePlayer inside other websites using an <code>&lt;embed&gt;<\/code> element.<\/p>\n\n\n\n<p>WavePlayer 2.3.1 comes already with this capability, but you need to add an <strong>embed-attachment.php<\/strong> file to your <strong><a href=\"https:\/\/developer.wordpress.org\/themes\/advanced-topics\/child-themes\/\" rel=\"noopener\">Child Theme<\/a><\/strong>.<\/p>\n\n\n\n<p>When WordPress finds that file in your Theme root folder, it uses it to display an element from your website inside an <code>&lt;iframe&gt;<\/code>. If you want to read more about this technique, you can refer to <a href=\"https:\/\/developer.wordpress.org\/themes\/basics\/template-hierarchy\/#embeds\" rel=\"noopener\"><strong>this article<\/strong><\/a> in the WordPress documentation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The <code>embed-attachment.php<\/code> template file<\/h3>\n\n\n\n<p>Let&#8217;s start with a basic structure of the embed-attachment.php file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nget_header( 'embed' );\n\nif ( have_posts() ) {\n    while ( have_posts() ) {\n        the_post();\n        echo do_shortcode(\"[ waveplayer ids='\" . get_the_ID() . \"']\");\n    }\n} else {\n    get_template_part( 'embed', '404' );\n}\n\nget_footer( 'embed' );\n?><\/pre>\n\n\n\n<p>Compared to the usual single.php, the file <code>embed.php<\/code> (or, in this case, <code>embed-attachment.php<\/code>) adds the name &#8217;embed&#8217; to <code>get_header()<\/code> and <code>get_footer()<\/code>. We then see the main query looping among all the results. Since this is going to be used on a single attachment post, the query is going to return only one result: the page of the attachment we want to embed inside an external website.<\/p>\n\n\n\n<p>How do we get the right link to be used in the embed element? It is pretty easy. Just go to the <strong>Media library<\/strong> and click on the thumbnail of the audio attachment you want to embed. In the right bottom corner, look for the link &#8220;<strong>View Attachment Page<\/strong>&#8220;, right click on it and choose &#8220;<strong>Copy link address<\/strong>&#8221; (or, alternatively, click on it and copy the URL in the address bar of the browser).<\/p>\n\n\n\n<p>Now, it is just a matter of typing the right embed code inside the website where we want to publish our attachment. The embed code looks like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;embed height=\"220\" src=\"https:\/\/www.waveplayer.info\/10197133_wedding_by_audiopizza_preview\/?embed=true\"><\/pre>\n\n\n\n<p>Please note that, in order for this to work, it is important that you add <code>embed=true<\/code> at the end of the link. You need also to pay attention to the structure of the URL you copied. If the URL already contains some arguments, like the following one:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">www.waveplayer.info\/?post_type=attachment&amp;p=1614<\/pre>\n\n\n\n<p>then you have to add <code>embed=true<\/code> at the end of the URL prefixing it with an <strong>ampersand<\/strong> (<strong>&amp;<\/strong>) like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">www.waveplayer.info\/?post_type=attachment&amp;p=1614&amp;embed=true<\/pre>\n\n\n\n<p>On the contrary, if your URL is a permalink like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">www.waveplayer.info\/10197133_wedding_by_audiopizza_preview\/<\/pre>\n\n\n\n<p>you have to prefix <code>embed=true<\/code> with a <strong>question mark<\/strong> (<strong>?<\/strong>), like this:<br><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">www.waveplayer.info\/10197133_wedding_by_audiopizza_preview\/?embed=true<\/pre>\n\n\n\n<p>A simple rule of thumb to better understand whether you need to use a question mark or an ampersand is checking the URL you have before adding the embed argument. If the URL already contains a question mark, then it means you need to add the embed argument prefixing it with an ampersand. If the URL doesn&#8217;t contain a question mark, then you need to use that to add the embed argument.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Providing additional arguments<\/h3>\n\n\n\n<p>You can also pass additional arguments to the URL you embed, so that you can alter the aspect and the property of your embedded WavePlayer instance. For example, the following example pass the size and the shuffle argument to the embedded player:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">www.waveplayer.info\/10197133_wedding_by_audiopizza_preview\/?embed=true&amp;size=large&amp;shuffle<\/pre>\n\n\n\n<p>Of course, we have to make some adjustments to the embed-attachment.php file as well, so that we process the additional arguments. The following code retrieves the size and shuffle arguments from the URL and incorporates them into the shortcode:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\n$args = array();\nif ( isset( $_GET['size']) ) $args[] = \"size='$size'\";\nif ( isset( $_GET['shuffle'] ) ) $args[] = \"shuffle='true'\";\n\n$args = implode( \" \", $args);\n\nget_header( 'embed' );\nif ( have_posts() ) {\n    while ( have_posts() ) {\n        the_post();\n        echo do_shortcode(\"[ waveplayer $args ids='\".get_the_ID().\"']\");\n    }\n} else {\n    get_template_part( 'embed', '404' );\n}\nget_footer( 'embed' );\n?><\/pre>\n\n\n\n<p>By following the example above, you can add as many arguments as you like to the embedded URL, adjusting the WavePlayer instance to your needs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This example shows you how to embed an audio file in other websites using WavePlayer.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"class_list":["post-1662","post","type-post","status-publish","format-standard","hentry","category-docs"],"_links":{"self":[{"href":"https:\/\/www.waveplayer.info\/wp-json\/wp\/v2\/posts\/1662","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.waveplayer.info\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.waveplayer.info\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.waveplayer.info\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.waveplayer.info\/wp-json\/wp\/v2\/comments?post=1662"}],"version-history":[{"count":0,"href":"https:\/\/www.waveplayer.info\/wp-json\/wp\/v2\/posts\/1662\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.waveplayer.info\/wp-json\/wp\/v2\/media?parent=1662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.waveplayer.info\/wp-json\/wp\/v2\/categories?post=1662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.waveplayer.info\/wp-json\/wp\/v2\/tags?post=1662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}